生活随笔
收集整理的這篇文章主要介紹了
c/c++在windows下获取时间和计算时间差的几种方法总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、標準C和C++都可用
1、獲取時間用time_t?time( time_t * timer ),計算時間差使用double difftime( time_t timer1, time_t timer0 )。?精確到秒。
測試程序如下:
[c-sharp]?view plaincopy
#include?<time.h>??#include?<stdio.h>??int?main()??{??????time_t?start?,end?;??????double?cost;??????time(&start);??????sleep(1);??????time(&end);??????cost=difftime(end,start);??????printf("%f/n",cost);??????return?0;??}??
?
?? ?本程序在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秒。
?? ?測試程序如下:
[c-sharp]?view plaincopy
#include?<time.h>??#include?<stdio.h>??int?main()??{??????double?start,end,cost;??????start=clock();??????sleep(1);??????end=clock();??????cost=end-start;??????printf("%f/n",cost);??????return?0;??}??
?
二、C++中(此處針對windows環境,標準c中則linux和windows都可以)
1、GetTickCount()
?? ? ?調用函數需包含windows.h。得到的是系統運行的時間 精確到毫秒,測試程序如下:
[c-sharp]?view plaincopy
#include?<iostream>??#include?<windows.h>??using?namespace?std;??int?main()??{??????double?start?=?GetTickCount();??????Sleep(1000);??????double??end=GetTickCount();??????cout?<<?"GetTickCount:"?<<?end-start?<<?endl;??????????return?0;??}??
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即可)。當然,精度肯定為秒了。
測試程序如下:
[c-sharp]?view plaincopy
#include?<iostream>??#include?<windows.h>??using?namespace?std;??int?main()??{??????SYSTEMTIME?start;?//windows.h中??????GetLocalTime(&start);//time.h的tm結構體一樣的效果??????cout<<?start.year?<<?endl;??}??
?
c語言的gmtime方法的示范代碼如下:
[c-sharp] view plaincopy
#include?<time.h>??#include?<stdio.h>??#include?<stdlib.h>??int?main()??{??????struct?tm?*tm_ptr;??????time_t?the_time;??????(void)?time(&the_time);??????tm_ptr?=?gmtime(&the_time);??????printf("Raw?time?is?%ld/n",?the_time);??????printf("gmtime?gives:/n");??????printf("date:?%02d/%02d/%02d/n",???????????tm_ptr->tm_year,?tm_ptr->tm_mon+1,?tm_ptr->tm_mday);??????printf("time:?%02d:%02d:%02d/n",??????????tm_ptr->tm_hour,?tm_ptr->tm_min,?tm_ptr->tm_sec);??????exit(0);??}??
另外,c語言有類似于GetLocalTime方法的函數ctime()。
對localtime(),原型為:struct tm *localtime(const time_t *timep);將測試程序的gmtime改為localtime,則可以看到輸出的時間為爭取時間和日期了。為了更友好的得到時間和日期,像date那樣輸出,可以用asctime或ctime函數,原型:char ?*ctime(const time_t ?*timeval);測試代碼如下:
[c-sharp]?view plaincopy
#include?<time.h>??#include?<stdio.h>??#include?<stdlib.h>??int?main()??{??????time_t?the_time;??????time(&the_time);??????printf("The?date?is?:?%s?/n"?,?ctime(&the_time));??????exit(0);??}??
?
3、要獲取高精度時間,可以使用
?????? BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)獲取系統的計數器的頻率
?????? BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)獲取計數器的值
?????? 然后用兩次計數器的差除以Frequency就得到時間。
測試程序如下:
[c-sharp]?view plaincopy
#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,不過經驗證,可以不用包含)。測試代碼如下:
[c-sharp]?view plaincopy
#include?<iostream>??#include?<windows.h>//GetTickCount??//#include?<mmsystem.h>??using?namespace?std;??int?main()??{??????DWORD??start?=?timeGetTime();//??????Sleep(1000);??????DWORD??end=?timeGetTime();//??????cout?<<??timeGetTime()?<<?endl;??????return?0;??}??
?5、MFC中,
CTime::GetCurrentTime() 精確到秒,不列出測試代碼。
?
關于定時器什么的,目前用到地方不多,就不總結了
?
參考網址:
1、http://blog.csdn.net/wallaceli1981/archive/2009/10/24/4723218.aspx?
2、http://wenku.baidu.com/view/beb3c9eef8c75fbfc77db2b5.html
總結
以上是生活随笔為你收集整理的c/c++在windows下获取时间和计算时间差的几种方法总结的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。