`

计算程序的执行时间

阅读更多
在windows下计算一段程序的执行时间,有以下方法:
(1):使用clock()函数(需包含头文件time.h)
我的c程序代码如下:
/* computer execution time using clock() */
/* the prototype : clock_t clock(void); */
/* document url: msdn.microsoft.com/en-us/library/4e2ess30(VS.71).aspx */

#include <stdio.h>
#include <time.h>

#define LOOPNUM 35000

int Func(int n)
{
  int i,j;
  double result=0;
  for(i = 0; i < n; i++)
    for(j = 0; j < n; j++)
      result += i*j;
  return 0;
}
double MeasureTime(int (*f)(int),int n)
{
  clock_t start,end;
  double TotTime;
  start = clock();
  (*f)(n);
  end = clock();
  TotTime = (double)(end-start)/CLOCKS_PER_SEC;
  printf("CLOCKS_PER_SEC is %d\n",CLOCKS_PER_SEC);
  printf("The total time is %.5f\n",TotTime);
  return TotTime;
}
int main(void)
{
  MeasureTime(Func,LOOPNUM);
  return 0;
}

在我电脑上(速龙3000+,32位winxp)用mingw编译,输出结果为:
CLOCKS_PER_SEC is 1000
The total time is 9.75000


clock()函数返回时钟所走过的滴答数,其精度只能到毫秒(千分之一秒)。
(2)使用QueryPerformanceFrequencyQueryPerformanceCounter 函数(需包含头文件winbase.h)
我的c程序代码如下:
/* computer execution time using QueryPerformanceCounter()
 * and QueryPerformance Frequency()
 * head file need to be included <winbase.h>
 * function prototype:
 * http://msdn.microsoft.com/en-us/library/ms644904(v=VS.85).aspx
 *BOOL WINAPI QueryPerformanceCounter(__out LARGE_INTEGER *lpPerformanceCount);
 *BOOL WINAPI QueryPerformanceFrequency(__out LARGE_INTEFER *lpFrequency);
 */
/* LARGE_INTEGER definition(in winnt.h>:
typedef union _LARGE_INTEFER{
  struct{
    DWORD LowPart;
    LONG HighPart;
  }
  struct{
    DWORD LowPart;
    LONG HighPart;
  }u;
  LONGLONG QuadPart;
}LARGE_INTEFER,*PLARGE_INTEGER;
*/

#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <winbase.h>
/*  #include <winnt.h>*/

#define LOOPNUM 35000

int Func(int n)
{
  int i,j;
  double result=0;
  for(i = 0; i < n; i++)
    for(j = 0; j < n; j++)
      result += i*j;
  return 0;
}
double MeasureTime(int (*f)(int),int n)
{
  LONGLONG start,end;
  LARGE_INTEGER largeint;
  double TotTime,freq;
  QueryPerformanceFrequency(&largeint);
  freq = (double)largeint.QuadPart;
  QueryPerformanceCounter(&largeint);
  start = largeint.QuadPart;
  (*f)(n);
  QueryPerformanceCounter(&largeint);
  end = largeint.QuadPart;
  TotTime = (double)(end-start)/freq;
  printf("Performance frequency is %f\n",freq);
  printf("The total time is %.9F\n",TotTime);
  return TotTime;
}
int main(void)
{
  MeasureTime(Func,LOOPNUM);
  return 0;
}

在我电脑上(速龙3000+,32位winxp)用mingw编译,输出结果为:
Performance frequency is 3579545.000000
The total time is 9.832611687


这段代码写的很粗糙,没有考虑返回错误的情况。虽然我用mingw编译,但是用其他的工具(VisualStudio等)应该也没有问题的,这两个函数是属于windows函数,windows 2000 pro以后的操作系统应该都能使用此程序
另外,需要指出的是,windows下的QueryPerformanceCounter和QueryPerformanceFrequency统计时间的方法使用了一个被称为Time Stamp Counter的寄存器(从奔腾cpu时起即开始有该寄存器),可以直接用嵌入式汇编(或者有些IDE已经直接提供了对应的函数)读取该寄存器的值,然后计算程序执行时间。

我的这两个程序的Makefile代码如下:
CC=gcc
CFLAGS=-Wall -ansi -o
LDFLAGS=-lm
all:clock counter
clock:clock.c
	$(CC) $(CFLAGS) $@ $<
counter:counter.c
	$(CC) $(CFLAGS) $@ $<


至于在Linux下统计程序的执行时间,可以使用gettimeofday()(参见《数据结构与算法分析-C语言描述》习题2.6,它能实现微妙级(百万分之一秒)的准确度)或者clock_gettime()(可实现纳秒级(十亿分之一秒)的准确度)


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics