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

后端 未结 5 2079
太阳男子
太阳男子 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:24

    I am not expert in C++, but you have at least the following to affect performance:

    1. OS level caching for the file
    2. For Java you are using a buffered reader and the buffer size defaults to a page or something. I am not sure how C++ streams does this.
    3. Since the file is so big that JIT would probably be kicked in, and it probably compiles the Java byte code better than if you don't turn any optimization on for your C++ compiler.

    Since I/O cost is the major cost here, I guess 1 and 2 are the major reasons.

提交回复
热议问题