你会如何​​高效地实现尾巴? [英] How would you implement tail efficiently?

查看:80
本文介绍了你会如何​​高效地实现尾巴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是实现* NIX尾部的有效途径?
我想出了(写)有两个简单的解决方案,使用一种循环缓冲区的两个加载进线圆形结构(阵列|双向链表循环表 - 为了好玩)。
我见过老的实现的一部分,busybox的,从我的理解,他们用fseek的发现EOF,然后阅读的东西倒退。有什么更清洁,更快的在那里?
我被问过这个面试和提问者看起来并不满足。谢谢你在前进。

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 (array | doubly linked circular list - for fun). I've seen part of older implementation in busybox and from what I understood, they used fseek to find EOF and then read stuff "backwards". Is there anything cleaner and faster out there? I got asked this on interview and asker did not look satisfied. Thank you in advance.

推荐答案

我不认为还有比保持最新的N行,而读数据转发或从月底开始和倒着走,直到你不同的解决方案读取第N行。

I don't think there are solutions different than "keep the latest N lines while reading forward the data" or "start from the end and go backwards until you read the Nth line".

问题的关键是,你会使用一个或另一个根据上下文。

The point is that you'd use one or the another based on the context.

在去年底和倒退,是更好,当尾巴访问随机访问文件,或者当数据是足够小,可以上的内存放。
在这种情况下,运行时被最小化,因为在扫描具有要输出的数据(因此,它的最佳)

The "go to the end and go backwards" is better when tail accesses a random access file, or when the data is small enough to be put on memory. In this case the runtime is minimized, since you scan the data that has to be outputted (so, it's "optimal")

您溶液(保持N个最近线)是更好的,当尾部与一个管道或当数据是巨大的进料。
在这种情况下,其他溶液浪费太多存储器,所以它是不实用的,并且在该情况下,源是比尾(这是可能的)扫描所有的文件慢无所谓那么多。

Your solution (keep the N latest lines) is better when tail is fed with a pipeline or when the data is huge. In this case, the other solution wastes too much memory, so it is not practical and, in the case the source is slower than tail (which is probable) scanning all the file doesn't matter that much.

这篇关于你会如何​​高效地实现尾巴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆