c/c++在windows下獲取時間和計算時間差的幾種方法總結
一、標准C和C++都可用
1、獲取時間用time_t time( time_t * timer ),計算時間差使用double
difftime( time_t timer1, time_t timer0 )。 精確到秒。
測試程序如下:
1 #include <time.h> 2 #include <stdio.h> 3 4 int main() 5 { 6 time_t start, end; 7 double cost; 8 9 time(&start); 10 sleep(1); 11 time(&end); 12 cost=difftime(end,start); 13 printf("%f\n",cost); 14 15 return 0; 16 }
本程序在fedora9測試通過。
關於代碼中的sleep函數,需要注意的是:
1)在windows下,為Sleep()函數,且需包含windows.h
2)關於sleep中的數,在Windows和Linux下1000代表的含義並不相同,Windows下的表示1000毫秒,也就是1秒鍾;Linux下表示1000秒,Linux下使用毫秒級別的函數可以使用usleep。
2、clock_t clock()
clock() 獲取的是計算機啟動后的時間間隔,得到的是CPU時間,精確到
1/CLOCKS_PER_SEC秒。
測試程序如下:
1 #include <time.h> 2 #include <stdio.h> 3 4 int main() 5 { 6 double start, end, cost; 7 8 start=clock(); 9 sleep(1); 10 end=clock(); 11 cost=end-start; 12 printf("%f\n",cost); 13 14 return 0; 15 }
二、C++中(此處針對windows環境,標准c中則linux和windows都可以)
1、GetTickCount()
調用函數需包含windows.h。得到的是系統運行的時間 精確到毫秒,測試程序如下:
1 #include <iostream> 2 #include <windows.h> 3 4 using namespace std; 5 6 int main() 7 { 8 double start = GetTickCount(); 9 Sleep(1000); 10 double end=GetTickCount(); 11 cout << "GetTickCount:" << end-start << endl; 12 13 return 0; 14 }
2、GetLocalTime()
獲得的是結構體保存的year,month等信息。而C語言time函數獲得是從1970年1月1日0時0分0秒到此時的秒數。需要gmtime函數轉換為常用的日歷(返回的是世界時間,要顯示常用的時間,則為localtime函數)。
在c語言中,保存常用日歷的結構體為struct tm,包含在time.h中,c++語言為SYSTEMTIME結構體,包含在winbase.h(編程包含windows.h即可)。當然,精度肯定為秒了。
測試程序如下:
1 #include <iostream> 2 #include <windows.h> 3 4 using namespace std; 5 6 int main() 7 { 8 SYSTEMTIME start; //windows.h中 9 GetLocalTime(&start);//time.h的tm結構體一樣的效果 10 cout<< start.year << endl; 11 }
c語言的gmtime方法的示范代碼如下:
1 #include <time.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 int main() 6 { 7 struct tm *tm_ptr; 8 time_t the_time; 9 (void) time(&the_time); 10 11 tm_ptr = gmtime(&the_time); 12 printf("Raw time is %ld\n", the_time); 13 printf("gmtime gives:\n"); 14 printf("date: %02d/%02d/%02d\n", tm_ptr->tm_year, tm_ptr->tm_mon+1, tm_ptr->tm_mday); 15 printf("time: %02d:%02d:%02d\n", tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec); 16 exit(0); 17 }
另外,c語言類似於GetLocalTime方法的函數為ctime()。
對localtime(),原型為:struct tm *localtime(const time_t *timep);將測試程序的gmtime改為localtime,則可以看到輸出的時間為爭取時間和日期了。為了更友好的得到時間和日期,像date那樣輸出,可以用asctime或ctime函數,原型:char *ctime(const time_t *timeval);
測試程序如下:
1 #include <time.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 int main() 6 { 7 time_t the_time; 8 time(&the_time); 9 printf("The date is : %s \n" , ctime(&the_time)); 10 11 exit(0); 12 }
3、要獲取高精度時間,可以使用
BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)獲取系統的計數器的頻率
BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)獲取計數器的值
然后用兩次計數器的差除以Frequency就得到時間。
測試程序如下:
#include <iostream> #include <windows.h> using namespace std; int main() { LARGE_INTEGER m_nFreq; LARGE_INTEGER m_nBeginTime; LARGE_INTEGER nEndTime; QueryPerformanceFrequency(&m_nFreq); // 獲取時鍾周期 QueryPerformanceCounter(&m_nBeginTime); // 獲取時鍾計數 Sleep(100); QueryPerformanceCounter(&nEndTime); cout << (double)(nEndTime.QuadPart-m_nBeginTime.QuadPart)*1000/m_nFreq.QuadPart << endl; }
需要注意的就是結果需要強制轉換為double,不然會得到如下錯誤:<< is ambiguous。
4、timeGetTime()。
精度:毫秒,與GetTickCount()相當。使用需要包含windows.h,並加入Winmm.lib(雖然查到資料說需要包含mmsystem.h,不過經驗證,可以不用包含)。
測試程序如下:
1 #include <iostream> 2 #include <windows.h> 3 //#include <mmsystem.h> 4 5 using namespace std; 6 7 int main() 8 { 9 DWORD start = timeGetTime(); 10 Sleep(1000); 11 DWORD end= timeGetTime(); 12 cout << end-start << endl; 13 14 return 0; 15 }