gettimeofday 和clock_gettime 哪个更快?

发布于 2024-09-09 21:52:07 字数 36 浏览 6 评论 0原文

我想存储事件时间。 我找到了这两个函数,但不知道哪个更快。

I want to store event time.
I found these two functions, but don't know which one is faster.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蘸点软妹酱 2024-09-16 21:52:08

看看这个讨论。它看起来质量很好,并且与您可能正在寻找的内容非常相关,尽管有点过时。

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.

玩物 2024-09-16 21:52:07
int main()
{
    struct timespec tp;
    struct timeval tv;
    int i=0;
    int j=0;
    for(i=0; i < 500000; i++)
    {
        gettimeofday(&tv, NULL);
        j+= tv.tv_usec%2;
        clock_gettime(CLOCK_HIGHRES, &tp);
        j+=tp.tv_sec%2;
    }
    return 0;

}



 %Time Seconds Cumsecs  #Calls   msec/call  Name
  68.3    0.28    0.28  500000      0.0006  __clock_gettime
  22.0    0.09    0.37  500000      0.0002  _gettimeofday
   7.3    0.03    0.40 1000009      0.0000  _mcount
   2.4    0.01    0.41       1     10.      main
   0.0    0.00    0.41       4      0.      atexit
   0.0    0.00    0.41       1      0.      _exithandle
   0.0    0.00    0.41       1      0.      _fpsetsticky
   0.0    0.00    0.41       1      0.      _profil
   0.0    0.00    0.41       1      0.      exit

它在 Solaris 9 v440 机器上运行。在您自己的盒子上配置代码。此结果与之前提供的链接中引用的结果完全不同。换句话说,如果有差异,那是由于实施造成的。

int main()
{
    struct timespec tp;
    struct timeval tv;
    int i=0;
    int j=0;
    for(i=0; i < 500000; i++)
    {
        gettimeofday(&tv, NULL);
        j+= tv.tv_usec%2;
        clock_gettime(CLOCK_HIGHRES, &tp);
        j+=tp.tv_sec%2;
    }
    return 0;

}



 %Time Seconds Cumsecs  #Calls   msec/call  Name
  68.3    0.28    0.28  500000      0.0006  __clock_gettime
  22.0    0.09    0.37  500000      0.0002  _gettimeofday
   7.3    0.03    0.40 1000009      0.0000  _mcount
   2.4    0.01    0.41       1     10.      main
   0.0    0.00    0.41       4      0.      atexit
   0.0    0.00    0.41       1      0.      _exithandle
   0.0    0.00    0.41       1      0.      _fpsetsticky
   0.0    0.00    0.41       1      0.      _profil
   0.0    0.00    0.41       1      0.      exit

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.

追我者格杀勿论 2024-09-16 21:52:07

这取决于您的系统;没有哪个标准更快。我的猜测是它们通常都具有相同的速度。 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.

孤君无依 2024-09-16 21:52:07

取决于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 are CLOCK_*_COARSE constants. These timers are fastest, but are not precise.

gettimeofday() should return the same as clock_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.

夜光 2024-09-16 21:52:07

你不在乎。

如果你经常给他们中的任何一个打电话,以至于这很重要,那么你就错了。

分析导致特定性能问题的代码并检查它在那里花费了多少时间。如果太多,请考虑重构它以减少调用该函数的频率。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文