在我們實際的編程工作中,經常要測量程序的運行時間,比如衡量算法的運行時間等等。在這裏我收集了網上集中測量程序運行時間的方法。
通過網上查閱資料,找到以下幾種VC中求取程序運行時間的方法:
方法一 利用GetTickCount函數(ms)
代碼:
CString str;
longt1=GetTickCount();//程序段開始前取得系統運行時間(ms)
。。。。。。//to do sth
longt2=GetTickCount();//程序段結束後取得系統運行時間(ms)
str.Format("time:%dms",t2-t1);//前後之差即程序運行時間
AfxMessageBox(str);
方法二利用C/C++計時函數(s)
代碼:
#include"time.h"
clock_t start, finish;
start =clock();
finish = clock();
printf("%f seconds\n",(double)(finish-start)/CLOCKS_PER_SEC);
方法三 利用CTime類 獲取系統時間
代碼:
CString str;
//獲取系統時間
CTime tm;
tm=CTime::GetCurrentTime();
str=tm.Format("現在時間是%Y年%m月%d日 %X");
AfxMessageBox(str);
方法四 利用GetLocalTime類獲取系統時間
SYSTEMTIME st;
CString strDate,strTime;
GetLocalTime(&st);
strDate.Format("M----",st.wYear,st.wMonth,st.wDay);
strTime.Format("-:-:-",st.wHour,st.wMinute,st.wSecond);
AfxMessageBox(strDate);
AfxMessageBox(strTime);
方法五 利用API函數
BOOL QueryPerformanceCounter( LARGE_INTEGER*lpPerformanceCount // counter value );