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

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

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

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

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

谢谢


当前回答

Thomas Pornin的回答是:

#define TICK(X) clock_t X = clock()
#define TOCK(X) printf("time %s: %g sec.\n", (#X), (double)(clock() - (X)) / CLOCKS_PER_SEC)

像这样使用它:

TICK(TIME_A);
functionA();
TOCK(TIME_A);

TICK(TIME_B);
functionB();
TOCK(TIME_B);

输出:

time TIME_A: 0.001652 sec.
time TIME_B: 0.004028 sec.

其他回答

我发现通常的clock(),这里每个人都推荐,由于某种原因在一次运行中严重偏离,即使对于静态代码也没有任何副作用,如绘制到屏幕或读取文件。这可能是因为CPU改变了功耗模式,操作系统给出了不同的优先级,等等…

因此,每次使用clock()都可靠地获得相同结果的唯一方法是在循环中多次运行测量的代码(持续几分钟),并采取预防措施防止编译器对其进行优化:现代编译器可以预先计算在循环中运行的代码而不会产生副作用,并将其移出循环。例如,每次迭代都使用随机输入。

在一个数组中收集了足够多的样本后,对数组进行排序,并取中间的元素,称为中位数。中位数比平均值好,因为它排除了极端偏差,比如反病毒病毒占用了所有CPU或操作系统进行了一些更新。

这里有一个简单的实用程序来测量C/ c++代码的执行性能,平均值接近中值:https://github.com/saniv/gauge

我自己仍然在寻找一种更健壮、更快的方法来度量代码。人们可能会尝试在没有任何操作系统的情况下在受控条件下运行代码,但这会产生不切实际的结果,因为在现实中操作系统确实涉及到。

x86有这些硬件性能计数器,包括实际执行的指令数量,但是如果没有操作系统的帮助,它们很难访问,很难解释,并且有自己的问题(http://archive.gamedev.net/archive/reference/articles/article213.html)。尽管如此,他们仍然可以帮助调查瓶颈的性质(数据访问或对数据的实际计算)。

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

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

希望能有所帮助。

的问候!

Perf工具更准确地用于收集和分析正在运行的程序。使用性能统计显示与正在执行的程序相关的所有信息。

不是所有的解都在我的系统里起作用。

我可以适应

#include <time.h>

double difftime(time_t time1, time_t time0);

冒泡排序和选择排序执行时间的比较 我有一个程序,比较冒泡排序和选择排序的执行时间。 要找出一个代码块的执行时间,计算该代码块之前和之后的时间

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