2

I need to write a file in c++. The content is token from a while loop so now I'm writing it line by line. Now I'm thinking that I can improve the writing time saving all the content in a variable and then write the file. Does someone know which of the two ways is better?

Each line is written by this function:

void writeFile(char* filename, string value){
        ofstream outFile(filename, ios::app);
        outFile << value;
        outFile.close();
}

while(/*    Something   */){
   /*   something   */
   writeFile(..);

}

The other way is:

void writeNewFile(char* filename, string value){
    ofstream outFile(filename);
    outFile<<value;
    outFile.close();
}

string res = "";
while(/*    Something   */){
   /*   something   */
   res += mydata;

}
writeNewFile(filename, res);
Carme
  • 141
  • 1
  • 9
  • File I/O speed is not that simple a thing to try and optimize. Sure, if your program just does nonsensical things that would slow down disk writes (like call `sleep` or something like that) that's one thing. But if you've written code that seems ok but still has slow disk writes, then it could be your disk itself is slow. – PaulMcKenzie Apr 07 '16 at 17:42

3 Answers3

7

Have you considered;

ofstream outFile(filename);
while(/*    Something   */){
   /*...*/    
   outFile<< mydata;
}
outFile.close();

The outfile (streams) are buffered, which means that it will accumulate the data in an internal buffer (like a string) before writing it to disk -- you are unlikely to be able to beat that unless you have very special requirements

Soren
  • 14,402
  • 4
  • 41
  • 67
2

In the first case you are closing the filestream every loop iteration and opening a new stream, it'd be better to do something like:

void writeFile(const std::string& value, std::ostream* os = nullptr) {
    if (os != nullptr) {
        os << value;
    }
}

Or:

void writeFile(const std::string& value, std::ostream& os) {
    os << value;
}

And then call this with the fstream object (or its address in first case) you created in another function/main/whatever.

As for whether it's quicker to write continuously or at the end, it really depends on the kind of computations you're performing in the while loop and how much that'll slow down the whole process. However, for reliability reasons, sometimes it's best to write continuously to avoid losing all the data if the program crashes during the while loop execution for whatever reason.

sjrowlinson
  • 3,297
  • 1
  • 18
  • 35
1

fstream objects are already buffered, therfore adding to string doesn't help you much, in fact one may even lose performance as result of reallocation of string content when exceeding previously allocated size. One would have to test to know.

Werner Erasmus
  • 3,988
  • 17
  • 31