Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing data to a file in C++ - most efficient way?

In my current project I'm dealing with a big amount of data which is being generated on-the-run by means of a "while" loop. I want to write the data onto a CSV file, and I don't know what's better - should I store all the values in a vector array and write to the file at the end, or write in every iteration?

I guess the first choice it's better, but I'd like an elaborated answer if that's possible. Thank you.

like image 298
Arnaugir Avatar asked Nov 10 '22 10:11

Arnaugir


1 Answers

Make sure that you're using an I/O library with buffering enabled, and then write every iteration.

This way your computer can start doing disk access in parallel with the remaining computations.

PS. Don't do anything crazy like flushing after each write, or opening and closing the file each iteration. That would kill efficiency.

like image 120
Ben Voigt Avatar answered Dec 04 '22 01:12

Ben Voigt