计算程序的执行时间
在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](可實現納秒級(十億分之一秒)的準確度)
(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](可實現納秒級(十億分之一秒)的準確度)
總結
- 上一篇: ubuntu20.10创建QT应用程序快
- 下一篇: mysql 数据库文件导入和导出、远程上