Printing to the console vs writing to a file (speed)

后端 未结 4 984
执笔经年
执笔经年 2020-12-16 13:36

In C++, which would be faster if repeated, say, 5000 times:

cout << \"text!\" << endl;

or

my_text_file <<         


        
4条回答
  •  独厮守ぢ
    2020-12-16 14:26

    Writing the same amount of data, with the same buffer size to the console will most definitely be faster than writing to a file.

    You can speed up your write speed (both for console output, and file output) by not writing out the buffer with every line (i.e.- don't use std::endl after every line, as it both adds an endline to the stream, and writes the buffer). Instead use "\n" unless you need to ensure the buffer is output for some reason.

提交回复
热议问题