Execution Time

I have a simuation code. Where I write a lot of data into the file. And I don't close the file until I am done. Apart from that, I write one line at a time. Will the execution be faster if I write multiple lines instead of one at a time. If yes, why??


P.S.: To actually test it, I have to change my code at many places, so I just want to confirm it before I make any changes.

Thanks in advance.
It depends on what you're using to write to the file and if it's buffered and the size of the buffer.
Last edited on
kbw is right, though to make the decision easier for you, consider only how many system calls you are invoking. You can approximate this by saying that the max number of systems calls is equal to the number of lines in the file. If we're talking hundreds of thousands, then yes, you might see a benefit.

Try writing a simple program to test. Write X number of lines of random strings to a file and time it. If using Un*x, you can use the time command.

I am opening a file and then simply doing

1
2
3
4
ofstream fout;

fout<<str<<endl;


And this file size is around 22GB which takes around 2 hrs to write it into the file. :(.
Adding a couple of things to the discussion:

try writting data multiples of your page size, it will be fast.
this will happen becuase file writing is kernel request. to write to a file there is a request to kernel which is may times slow as compared to local calculations in your program.
you can calculate the total time this way:
1
2
3
4
5
double start_time = clock()
..
..
double end_time = clock();
double elapsed_time = (end_time - start_time) / CLOCKS_PER_SEC;
To what medium are you writing the data? Hard disk?

It is probably more efficient to write in multiples of the cluster size, not the page size, since the cluster is the smallest unit readable/writable by the kernel.

Also, what OS?
Topic archived. No new replies allowed.