How would you implement tail efficiently?

后端 未结 4 674
执笔经年
执笔经年 2020-12-14 20:42

What is the efficient way to implement tail in *NIX? I came up (wrote) with two simple solution, both using kind of circular buffer to load lines into circular structure (ar

4条回答
  •  青春惊慌失措
    2020-12-14 21:33

    First use fseek to find the end-of-file then subtract 512 and fseek to that offset, then read forward from there to end. Count the number of line-breaks because if there are too few you will have to do the same with a subtracted offset of 1024 ... but in 99% of cases 512 will be enough.

    This (1) avoids reading the whole file forward and (2) the reason why this is probably more efficient than reading backwards from the end is that reading forward is typically faster.

提交回复
热议问题