Is std::ifstream significantly slower than FILE?

后端 未结 6 1820
余生分开走
余生分开走 2020-12-01 11:07

I\'ve been informed that my library is slower than it should be, on the order of 30+ times too slow parsing a particular file (text file, size 326 kb). The user suggested th

6条回答
  •  眼角桃花
    2020-12-01 12:03

    I don't think that'd make a difference. Especially if you're reading char by char, the overhead of I/O is likely to completely dominate anything else. Why do you read single bytes at a time? You know how extremely inefficient it is?

    On a 326kb file, the fastest solution will most likely be to just read it into memory at once.

    The difference between std::ifstream and the C equivalents, is basically a virtual function call or two. It may make a difference if executed a few tens of million times per second, otherwise, not reall. file I/O is generally so slow that the API used to access it doesn't really matter. What matters far more is the read/write pattern. Lots of seeks are bad, sequential reads/writes good.

提交回复
热议问题