C程序的执行时间

我有一个C程序,旨在在多个处理器上并行运行。 我需要能够logging执行时间(可以是从1秒到几分钟的任何地方)。 我已经寻找答案,但他们似乎都build议使用clock()函数,然后计算程序所用的clock()数除以Clocks_per_second值。

我不确定Clocks_per_second值是如何计算的?

在Java中,我只是以毫秒为单位来执行当前时间。

C中有类似的东西吗? 我看了一下,但似乎找不到比第二个解决办法更好的方法。

我也知道一个分析器将是一个选项,但我期待自己实现一个计时器。

谢谢

CLOCKS_PER_SEC是在<time.h>声明的<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; 

请注意,这将以浮点types的forms返回时间。 这可以比一秒更精确(例如,你测量4.52秒)。 精度取决于架构; 在现代系统上,你很容易得到10ms或更低,但是在较老的Windows机器上(从Win98时代开始)则接近60ms。

clock()是标准C; 它“遍地开花”。 有类似系统的function,例如Unix类系统上的getrusage()

Java的System.currentTimeMillis()不测量同样的事情。 这是一个“挂钟”:它可以帮助您测量程序执行所花费的时间,但不会告诉您使用了多lessCPU时间。 在多任务系统(即所有这些系统)上,这些可以有很大的不同。

如果您正在使用Unix shell进行运行,则可以使用time命令。

 $ time ./a.out 

假设a.out作为可执行文件将给你运行这个所需的时间

在普通的香草C:

 #include <time.h> #include <stdio.h> int main() { clock_t tic = clock(); my_expensive_function_which_can_spawn_threads(); clock_t toc = clock(); printf("Elapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC); return 0; } 

你在function上需要这个:

 #include <sys/time.h> struct timeval tv1, tv2; gettimeofday(&tv1, NULL); /* stuff to do! */ gettimeofday(&tv2, NULL); printf ("Total time = %f seconds\n", (double) (tv2.tv_usec - tv1.tv_usec) / 1000000 + (double) (tv2.tv_sec - tv1.tv_sec)); 

请注意,这是以微秒为单位的度量,而不仅仅是秒。

大多数简单的程序都有以毫秒为单位的计算时间。 所以,我想,你会发现这有用。

 #include <time.h> #include <stdio.h> int main(){ clock_t start = clock(); // Execuatable code clock_t stop = clock(); double elapsed = (double)(stop - start) * 1000.0 / CLOCKS_PER_SEC; printf("Time elapsed in ms: %f", elapsed); } 

如果你想计算整个程序的运行时间,并且你在一个Unix系统上,用这个时间命令运行你的程序time ./a.out

很多答案都build议clock() ,然后CLOCKS_PER_SEC from time.h 这可能是一个坏主意,因为这是我的/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,这取决于你用什么选项来编译,因此它似乎不是一个好的解决scheme。

您必须考虑到,测量执行程序的时间取决于机器在特定时刻的负载。

知道在C中获得当前时间的方式可以通过不同的方式实现,更容易的是:

 #include <time.h> #define CPU_TIME (getrusage(RUSAGE_SELF,&ruse), ruse.ru_utime.tv_sec + \ ruse.ru_stime.tv_sec + 1e-6 * \ (ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec)) int main(void) { time_t start, end; double first, second; // Save user and CPU start time time(&start); first = CPU_TIME; // Perform operations ... // Save end time time(&end); second = CPU_TIME; printf("cpu : %.2f secs\n", second - first); printf("user : %d secs\n", (int)(end - start)); } 

希望能帮助到你。

问候!

ANSI C仅指定第二个精度时间函数。 但是,如果您在POSIX环境中运行,则可以使用gettimeofday()函数,该函数提供自UNIX Epoch以来经过的时间的微秒分辨率。

作为一个方面说明,我不推荐使用clock(),因为它在很多(如果不是全部的话)系统上执行得不好,而且不准确,除了它只涉及程序花费在CPU上的时间和而不是程序的总生命周期,根据你的问题,这是我想你想测量的。

每个解决scheme都不在我的系统中工作。

我可以使用

 #include <time.h> double difftime(time_t time1, time_t time0); 
  #include<time.h> #include<stdio.h> int main(){ clock_t begin=clock(); int i; for(i=0;i<100000;i++){ printf("%d",i); } clock_t end=clock(); printf("Time taken:%lf",(double)(end-begin)/CLOCKS_PER_SEC); } 

这个程序将像魅力一样工作。

(如果你的系统pipe理员改变系统时间,或者你的时区有不同的冬季和夏季时间,这里所有的答案都是缺乏的,所以…)

在Linux上使用: clock_gettime(CLOCK_MONOTONIC_RAW, &time_variable); 如果系统pipe理员改变时间,或者你生活在一个与夏令时间不同的冬季的国家,则不受影响。

 #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 (eg, if the system administrator manually changes the clock), but is affected by the incremental adjustments performed by adjtime(3) and NTP. 

气泡sorting和selectsorting的执行时间的比较我有一个比较气泡sorting和selectsorting的执行时间的程序。 为了找出执行一段代码的时间,计算块前后的时间

  clock_t start=clock(); … clock_t end=clock(); CLOCKS_PER_SEC is constant in time.h library 

示例代码:

 #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int a[10000],i,j,min,temp; for(i=0;i<10000;i++) { a[i]=rand()%10000; } //The bubble Sort clock_t start,end; start=clock(); for(i=0;i<10000;i++) { for(j=i+1;j<10000;j++) { if(a[i]>a[j]) { int temp=a[i]; a[i]=a[j]; a[j]=temp; } } } end=clock(); double extime=(double) (end-start)/CLOCKS_PER_SEC; printf("\n\tExecution time for the bubble sort is %f seconds\n ",extime); for(i=0;i<10000;i++) { a[i]=rand()%10000; } clock_t start1,end1; start1=clock(); // The Selection Sort for(i=0;i<10000;i++) { min=i; for(j=i+1;j<10000;j++) { if(a[min]>a[j]) { min=j; } } temp=a[min]; a[min]=a[i]; a[i]=temp; } end1=clock(); double extime1=(double) (end1-start1)/CLOCKS_PER_SEC; printf("\n"); printf("\tExecution time for the selection sort is %f seconds\n\n", extime1); if(extime1<extime) printf("\tSelection sort is faster than Bubble sort by %f seconds\n\n", extime - extime1); else if(extime1>extime) printf("\tBubble sort is faster than Selection sort by %f seconds\n\n", extime1 - extime); else printf("\tBoth algorithms have the same execution time\n\n"); } 
Interesting Posts