Fastest way to write 6k+ rows

I tried to find a post similar like this... but i didnt found it...

So i hope is not resolved in other thread.

Well I need to write around 6k information rows in a .txt every 10 mins
As time is critical.. I was wondering which method is the fastest.

Right now im using something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
toWrite(pbuf){
...
  CStdioFile file;
  file.Write( pbuf, pbuf.GetLength());
  ...
}

docTxt(){
  while(count < max ){
   ...
    while(j < i){
    ...
    pbuf = "XxxXx";... 
    }
   toWrite(pbuf);
  }
}


Maybe someone could give me an advice about which are my options.

Thanks in advance

Regards

Writing all the data to a temporary buffer and then writing the buffer in one go is definitely the fastest method.
6K rows in 10 minutes means 10 rows per second. If you can't write 10 rows per second, i think your performance problem is somewhere else than in the writes themselves
Depends on how long these rows are.
closed account (1vRz3TCk)
bartoli wrote:
6K rows in 10 minutes means 10 rows per second. If you can't write 10 rows per second, i think your performance problem is somewhere else than in the writes themselves
If you have to write 6K rows every 10 minutes and 'other stuff' keeps you occupied for 9 minutes and 50 seconds, you don't have much time to write your 6K lines.
If he is in that case, he can put the writing code in another thread since it is occupying disk access but not cpu time.
You may be able to improve your write times by replacing the default buffer with a larger one of your own:
1
2
3
4
5
6
7
8
9
10
11
12
13
14

int main()
{
	std::ofstream ofs;

	char* buff = new char[4096]; // big buffer
	ofs.rdbuf()->pubsetbuf(buff, 4096);

	ofs.open("outputfile.txt");

	// etc...

	delete[] buff;
}
Last edited on
Thanks all for the answers.

Ill try what you told me and see which are the results.

The whole program is in real time... thats why time is critical my part of the program can't spend more than 1-2 seconds as lots of other things has to be done at same time.

I really appreciate your help.

Regards.
Last edited on
Topic archived. No new replies allowed.