日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

计算程序的执行时间

發布時間:2025/3/11 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 计算程序的执行时间 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在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](可實現納秒級(十億分之一秒)的準確度)

總結

以上是生活随笔為你收集整理的计算程序的执行时间的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。