900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 计算程序的执行时间

计算程序的执行时间

时间:2020-11-03 12:41:55

相关推荐

计算程序的执行时间

在windows下计算一段程序的执行时间,有以下方法:

(1):使用[url=/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: /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=/en-us/library/ms644905%28VS.85%29.aspx]QueryPerformanceFrequency[/url]和[url=/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:

* /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=/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=/onlinepubs/009695399/functions/gettimeofday.html]gettimeofday()[/url](参见[url=/blog/1058806]《数据结构与算法分析-C语言描述》习题2.6[/url],它能实现微妙级(百万分之一秒)的准确度)或者[url=/onlinepubs/9699919799/functions/clock_gettime.html]clock_gettime()[/url](可实现纳秒级(十亿分之一秒)的准确度)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。