C语言 C - gettimeofday 计算时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5362577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:06:23  来源:igfitidea点击:

C - gettimeofday for computing time?

c

提问by Waypoint

do you know how to use gettimeofday for measuring computing time? I can measure one time by this code:

你知道如何使用 gettimeofday 来测量计算时间吗?我可以通过以下代码测量一次:

  char buffer[30];
  struct timeval tv;

  time_t curtime;



 gettimeofday(&tv, NULL); 
 curtime=tv.tv_sec;

 strftime(buffer,30,"%m-%d-%Y  %T.",localtime(&curtime));
 printf("%s%ld\n",buffer,tv.tv_usec);

This one is made before computing, second one after. But do you know how to subtracts it?

这个是在计算之前制作的,第二个是在计算之后制作的。但是你知道如何减去它吗?

I need result in miliseconds

我需要几毫秒的结果

采纳答案by Borealid

Your curtimevariable holds the number of seconds since the epoch. If you get one before and one after, the later one minus the earlier one is the elapsed time in seconds. You can subtract time_tvalues just fine.

您的curtime变量保存自纪元以来的秒数。如果你得到一个之前和一个之后,后一个减去前一个就是经过的时间(以秒为单位)。您可以time_t很好地减去值。

回答by R.. GitHub STOP HELPING ICE

To subtract timevals:

减去时间值:

gettimeofday(&t0, 0);
/* ... */
gettimeofday(&t1, 0);
long elapsed = (t1.tv_sec-t0.tv_sec)*1000000 + t1.tv_usec-t0.tv_usec;

This is assuming you'll be working with intervals shorter than ~2000 seconds, at which point the arithmetic may overflow depending on the types used. If you need to work with longer intervals just change the last line to:

这是假设您将使用短于 ~2000 秒的间隔,此时算术可能会溢出,具体取决于所使用的类型。如果您需要使用更长的时间间隔,只需将最后一行更改为:

long long elapsed = (t1.tv_sec-t0.tv_sec)*1000000LL + t1.tv_usec-t0.tv_usec;

回答by Xofo

The answer offered by @Daniel Kamil Kozar is the correct answer - gettimeofday actually should not be used to measure the elapsed time. Use clock_gettime(CLOCK_MONOTONIC) instead.

@Daniel Kamil Kozar 提供的答案是正确的答案 - 实际上不应该使用 gettimeofday 来测量经过的时间。改用clock_gettime(CLOCK_MONOTONIC)。



Man Pages say - The time returned by gettimeofday() is affected by discontinuous jumps in the system time (e.g., if the system administrator manually changes the system time). If you need a monotonically increasing clock, see clock_gettime(2).

手册页说 - gettimeofday() 返回的时间受系统时间不连续跳跃的影响(例如,如果系统管理员手动更改系统时间)。如果您需要单调递增的时钟,请参阅clock_gettime(2)。

The Opengroup says - Applications should use the clock_gettime() function instead of the obsolescent gettimeofday() function.

Opengroup 说 - 应用程序应该使用 clock_gettime() 函数而不是过时的 gettimeofday() 函数。

Everyone seems to love gettimeofday until they run into a case where it does not work or is not there (VxWorks) ... clock_gettime is fantastically awesome and portable.

每个人似乎都喜欢 gettimeofday,直到他们遇到它不起作用或不存在的情况(VxWorks)……clock_gettime 非常棒且可移植。

<<

<<

回答by Armen Tsirunyan

If you want to measure code efficiency, or in any other way measure time intervals, the following will be easier:

如果你想衡量代码效率,或者以任何其他方式衡量时间间隔,下面会更容易:

#include <time.h>

int main()
{
   clock_t start = clock();
   //... do work here
   clock_t end = clock();
   double time_elapsed_in_seconds = (end - start)/(double)CLOCKS_PER_SEC;
   return 0;
}

hth

回答by Thomas

No. gettimeofday should NEVER be used to measure time.

不。gettimeofday 永远不应该用于测量时间

This is causing bugs all over the place. Please don't add more bugs.

这导致到处都是错误。请不要添加更多错误。