Performance Difference Between C and C++ Style File IO

前端 未结 3 816
萌比男神i
萌比男神i 2020-12-02 20:51

I\'ve always heard that C++ file I/O operations are much much slower then C style I/O. But I didn\'t find any practical references on comparatively how slow they actually ar

3条回答
  •  被撕碎了的回忆
    2020-12-02 21:17

    You're using endl to print a newline. That is the problem here, as it does more than just printing a newline — endl also flushes the buffer which is an expensive operation (if you do that in each iteration).

    Use \n if you mean so:

    file << i << '\n';
    

    And also, must compile your code in release mode (i.e turn on the optimizations).

提交回复
热议问题