Fastest way to find the number of lines in a text (C++)

后端 未结 8 942
臣服心动
臣服心动 2020-12-07 23:12

I need to read the number of lines in a file before doing some operations on that file. When I try to read the file and increment the line_count variable at each iteration u

8条回答
  •  一生所求
    2020-12-07 23:59

    It isn't slow because of your algorithm , It is slow because IO operations are slow. I suppose you are using a simple O(n) algorithm that is simply going over the file sequentially. In that case , there is no faster algorithm that can optimize your program.

    However , I said there is no faster algorithm , but there is a faster mechanism which called "Memory Mapped file " , There are some drawback for mapped files and it might not be appropiate for you case , So you'll have to read about it and figure out by yourself.

    Memory mapped files won't let you implement an algorithm better then O(n) but it may will reduce IO access time.

提交回复
热议问题