Very surprising perfs of fprintf vs std::ofstream (fprintf is very slow)

前端 未结 5 1739
梦毁少年i
梦毁少年i 2021-02-15 17:15

I was running some benchmarks to find the most efficient way to write a huge array to a file in C++ (more than 1Go in ASCII).

So I compared std::ofstream with fprintf (s

5条回答
  •  不思量自难忘°
    2021-02-15 17:42

    Well, fprintf() does have to do a bit more work at runtime, since it has to parse and process the format string. However, given the size of your output file I would expect those differences to be of little consequence, and would expect the code to be I/O bound.

    I therefore suspect that your benchmark is flawed in some way.

    1. Do you consistently get a 12x difference if you run the tests repeatedly?
    2. What happens to the timings if you reverse the order in which you run the tests?
    3. What happens if you call fsync()/sync() at the end?

提交回复
热议问题