Why does Java read a big file faster than C++?

后端 未结 5 2064
太阳男子
太阳男子 2021-01-31 14:19

I have a 2 GB file (iputfile.txt) in which every line in the file is a word, just like:

apple
red
beautiful
smell
spark
input

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-31 14:26

    I would suspect that the main difference is that java.io.BufferedReader performs better than the std::ifstream because it buffers, while the ifsteam does not. The BufferedReader reads large chunks of the file in advance and hands them to your program from RAM when you call readLine(), while the std::ifstream only reads a few bytes at a time when you prompt it to by calling the >>-operator.

    Sequential access of large amounts of data from the hard drive is usually much faster than accessing many small chunks one at a time.

    A fairer comparison would be to compare std::ifstream to the unbuffered java.io.FileReader.

提交回复
热议问题