我有一个C程序,目的是在几个处理器上并行运行。我需要能够记录执行时间(可以从1秒到几分钟不等)。我已经搜索了答案,但它们似乎都建议使用clock()函数,然后涉及计算程序所用的时钟数除以Clocks_per_second值。
我不确定Clocks_per_second值是如何计算的?
在Java中,我只是在执行前后以毫秒为单位获取当前时间。
C语言中也有类似的东西吗?我看了一下,但我似乎找不到比第二次分辨率更好的方法。
我也知道一个分析器将是一个选项,但我希望自己实现一个定时器。
谢谢
CLOCKS_PER_SEC是一个在<time.h>中声明的常量。要获得C应用程序中任务使用的CPU时间,请使用:
clock_t begin = clock();
/* here, do your time-consuming job */
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
注意,这将以浮点类型返回时间。这可以比一秒更精确(例如,你测量的是4.52秒)。精度取决于架构;在现代系统上,你很容易得到10毫秒或更低,但在老式的Windows机器上(从Win98时代开始),它接近60毫秒。
clock()是标准C;它“无处不在”。有一些系统特定的函数,比如类unix系统上的getrusage()。
Java的System.currentTimeMillis()没有测量相同的东西。它是一个“挂钟”:它可以帮助您测量程序执行所花费的时间,但它不会告诉您使用了多少CPU时间。在多任务系统(即所有系统)上,这些可能有很大的不同。
(如果您的系统管理员更改了系统时间,或者您的时区有不同的冬季和夏季时间,这里就没有所有的答案。因此…)
在linux上使用:clock_gettime(clock_单调ic_raw, &time_variable);
如果系统管理员改变了时间,或者你生活在一个冬季和夏季不同的国家,等等,它不会受到影响。
#include <stdio.h>
#include <time.h>
#include <unistd.h> /* for sleep() */
int main() {
struct timespec begin, end;
clock_gettime(CLOCK_MONOTONIC_RAW, &begin);
sleep(1); // waste some time
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
printf ("Total time = %f seconds\n",
(end.tv_nsec - begin.tv_nsec) / 1000000000.0 +
(end.tv_sec - begin.tv_sec));
}
Man clock_gettime声明:
CLOCK_MONOTONIC
Clock that cannot be set and represents monotonic time since some unspecified starting point. This clock is not affected by discontinuous jumps in the system time
(e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments performed by adjtime(3) and NTP.
很多答案都建议使用clock(),然后是time.h中的CLOCKS_PER_SEC。这可能是一个坏主意,因为这是我的/bits/time.h文件所写的:
/* ISO/IEC 9899:1990 7.12.1: <time.h>
The macro `CLOCKS_PER_SEC' is the number per second of the value
returned by the `clock' function. */
/* CAE XSH, Issue 4, Version 2: <time.h>
The value of CLOCKS_PER_SEC is required to be 1 million on all
XSI-conformant systems. */
# define CLOCKS_PER_SEC 1000000l
# if !defined __STRICT_ANSI__ && !defined __USE_XOPEN2K
/* Even though CLOCKS_PER_SEC has such a strange value CLK_TCK
presents the real value for clock ticks per second for the system. */
# include <bits/types.h>
extern long int __sysconf (int);
# define CLK_TCK ((__clock_t) __sysconf (2)) /* 2 is _SC_CLK_TCK */
# endif
因此,CLOCKS_PER_SEC可能定义为1000000,这取决于用于编译的选项,因此它似乎不是一个好的解决方案。
有些人可能会发现另一种输入方式很有用:我在大学课程中用NVidia CUDA进行gpgpu编程时学过这种测量时间的方法(课程描述)。它结合了在以前的文章中看到的方法,我只是简单地发布它,因为需求赋予它可信度:
unsigned long int elapsed;
struct timeval t_start, t_end, t_diff;
gettimeofday(&t_start, NULL);
// perform computations ...
gettimeofday(&t_end, NULL);
timeval_subtract(&t_diff, &t_end, &t_start);
elapsed = (t_diff.tv_sec*1e6 + t_diff.tv_usec);
printf("GPU version runs in: %lu microsecs\n", elapsed);
我认为你可以乘以1.0 / 1000.0来得到适合你需要的测量单位。