我有一个C程序,目的是在几个处理器上并行运行。我需要能够记录执行时间(可以从1秒到几分钟不等)。我已经搜索了答案,但它们似乎都建议使用clock()函数,然后涉及计算程序所用的时钟数除以Clocks_per_second值。

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

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

C语言中也有类似的东西吗?我看了一下,但我似乎找不到比第二次分辨率更好的方法。

我也知道一个分析器将是一个选项,但我希望自己实现一个定时器。

谢谢


当前回答

(如果您的系统管理员更改了系统时间,或者您的时区有不同的冬季和夏季时间,这里就没有所有的答案。因此…)

在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.

其他回答

    #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);
    }

这个程序会很有效。

很多答案都建议使用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,这取决于用于编译的选项,因此它似乎不是一个好的解决方案。

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

#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命令运行你的程序,就像这个time ./a.out

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时间。在多任务系统(即所有系统)上,这些可能有很大的不同。

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

我们知道,在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));
}

希望能有所帮助。

的问候!