gettimeofday 和clock_gettime 哪个更快?
我想存储事件时间。 我找到了这两个函数,但不知道哪个更快。
I want to store event time.
I found these two functions, but don't know which one is faster.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看看这个讨论。它看起来质量很好,并且与您可能正在寻找的内容非常相关,尽管有点过时。
Have a look at this discussion. It seems of good quality and very closely related to what you might be looking for though a little dated.
它在 Solaris 9 v440 机器上运行。在您自己的盒子上配置代码。此结果与之前提供的链接中引用的结果完全不同。换句话说,如果有差异,那是由于实施造成的。
This ran on Solaris 9 v440 box. Profile the code on your own box. This result is completely different from the result quoted in a link supplied earlier. In other words, if there is a difference it will be due to the implmenetation.
这取决于您的系统;没有哪个标准更快。我的猜测是它们通常都具有相同的速度。
gettimeofday
更为传统,如果您希望代码能够移植到旧系统,这可能是一个不错的选择。clock_gettime
有更多功能。It will depend on your system; there's no standard for which is faster. My guess would be they're usually both about the same speed.
gettimeofday
is more traditional and probably a good bet if you want your code to be portable to older systems.clock_gettime
has more features.取决于
clock_gettime()
中使用的常量。对于最快的时钟,有CLOCK_*_COARSE
常量。这些计时器速度最快,但并不精确。gettimeofday()
应返回与clock_gettime(CLOCK_REALTIME)
相同的结果。此外,基准测试结果取决于体系结构(对于 Linux)。由于Linux有特殊技术(VDSO)来消除系统调用以获得时间。这些技术不适用于 X86-32 位架构。请参阅
strace
输出。Depending on constant, used in
clock_gettime()
. For the fastest clocks, there areCLOCK_*_COARSE
constants. These timers are fastest, but are not precise.gettimeofday()
should return the same asclock_gettime(CLOCK_REALTIME)
Also, benchmark results depend on architecture (for Linux). Since Linux has special technology (VDSO) to eliminate syscall to get time. These technologies do not work on X86-32bit architecture. See
strace
output.你不在乎。
如果你经常给他们中的任何一个打电话,以至于这很重要,那么你就错了。
分析导致特定性能问题的代码并检查它在那里花费了多少时间。如果太多,请考虑重构它以减少调用该函数的频率。
You don't care.
If you're calling either of them often enough that it matters, You're Doing It Wrong.
Profile code which is causing specific performance problems and check how much time it is spending in there. If it's too much, consider refactoring it to call the function less often.