POSIX lfind()/lsearch() 的性能比手动循环更好吗?
执行 lfind
/lsearch
比检查每个项目直到匹配的典型循环解决方案表现更好?这些功能的存在有什么特殊的理由吗?
Do lfind
/lsearch
perform better than a typical looping solution that checks each item until it matches? Is there any special sauce/reason that these functions exist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们可能并不比自制版本更高效,甚至可能会低一些,因为比较函数无法内联。
但这肯定不是他们的重点。它们完成了其他搜索功能的 API,特别是
bsearch
和tsearch
。Probably they are not more efficient that a homebrew version, maybe even a bit less since the comparison function can't be inlined.
But this is certainly not the point with them. They complete the API of the other search functions, in particular
bsearch
andtsearch
.措施!
您只能通过测量来了解性能。你的计算机和我的计算机上的情况绝对不同(我什至可能没有 POSIX 兼容编译器,无法自己测量
lfind
)。程序的不同运行之间的想法是不同的。因此,如果您需要知道,请尝试
lfind
和 homebrew 方法,并多次measure。无论如何...
C
库函数可能不是用C
编写的。如果您的lfind
是用Python
编写的,我敢打赌它会比自制方法慢:)Measure!
You can only know about performance by measuring. Things are definitely different on your computer and mine (I may even not have a POSIX compiant compiler cannot measure
lfind
myself). Thinks are different between different runs of a program.So, if you need to know, try both the
lfind
and homebrew methods and measure several times.Anyway ...
C
library functions may not have been written inC
. If yourlfind
was written inPython
I'd bet it would be slower than a homebrew method :)