计算程序的执行时间

在windows下计算一段程序的执行时间,有以下方法:
(1):使用[url=http://msdn.microsoft.com/en-us/library/4e2ess30%28VS.71%29.aspx]clock()[/url]函数(需包含头文件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编译,输出结果为:
[color=gray]CLOCKS_PER_SEC is 1000
The total time is 9.75000[/color]

clock()函数返回时钟所走过的滴答数,其精度只能到毫秒(千分之一秒)。
(2)使用[url=http://msdn.microsoft.com/en-us/library/ms644905%28VS.85%29.aspx]QueryPerformanceFrequency[/url]和[url=http://msdn.microsoft.com/en-us/library/ms644904%28v=VS.85%29.aspx]QueryPerformanceCounter[/url] 函数(需包含头文件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编译,输出结果为:
[color=gray]Performance frequency is 3579545.000000
The total time is 9.832611687[/color]

这段代码写的很粗糙,没有考虑返回错误的情况。虽然我用mingw编译,但是用其他的工具(VisualStudio等)应该也没有问题的,这两个函数是属于windows函数,windows 2000 pro以后的操作系统应该都能使用此程序
另外,需要指出的是,windows下的QueryPerformanceCounter和QueryPerformanceFrequency统计时间的方法使用了一个被称为[url=http://en.wikipedia.org/wiki/Time_Stamp_Counter]Time Stamp Counter[/url]的寄存器(从奔腾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下统计程序的执行时间,可以使用[url=http://pubs.opengroup.org/onlinepubs/009695399/functions/gettimeofday.html]gettimeofday()[/url](参见[url=http://codeprac.iteye.com/blog/1058806]《数据结构与算法分析-C语言描述》习题2.6[/url],它能实现微妙级(百万分之一秒)的准确度)或者[url=http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_gettime.html]clock_gettime()[/url](可实现纳秒级(十亿分之一秒)的准确度)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值