电竞比分网-中国电竞赛事及体育赛事平台

分享

時(shí)間的一些計(jì)算

 寂靜如河 2012-02-28

一、標(biāo)準(zhǔn)C和C++都可用

1、獲取時(shí)間用time_t time( time_t * timer ),計(jì)算時(shí)間差使用double difftime( time_t timer1, time_t timer0 )。 精確到秒。

測(cè)試程序如下:

  1. #include <time.h>  
  2. #include <stdio.h>   
  3. int main()  
  4. {  
  5.     time_t start ,end ;  
  6.     double cost;  
  7.     time(&start);  
  8.     sleep(1);  
  9.     time(&end);  
  10.     cost=difftime(end,start);  
  11.     printf("%f/n",cost);  
  12.     return 0;  
  13. }  
 

    本程序在fedora9測(cè)試通過(guò)。

    關(guān)于代碼中的sleep函數(shù),需要注意的是:

    1)在windows下,為Sleep函數(shù),且包含windows.h

    2)關(guān)于sleep中的數(shù),WindowsLinux1000代表的含義并不相同,Windows下的表示1000毫秒,也就是1秒鐘;Linux下表示1000秒,Linux下使用毫秒級(jí)別的函數(shù)可以使用usleep

 

2、clock_t clock(),clock()

    獲取的是計(jì)算機(jī)啟動(dòng)后的時(shí)間間隔,得到的是CPU時(shí)間,精確到1/CLOCKS_PER_SEC秒。

    測(cè)試程序如下:

  1. #include <time.h>  
  2. #include <stdio.h>   
  3. int main()  
  4. {  
  5.     double start,end,cost;  
  6.     start=clock();  
  7.     sleep(1);  
  8.     end=clock();  
  9.     cost=end-start;  
  10.     printf("%f/n",cost);  
  11.     return 0;  
  12. }  
 

二、C++中(此處針對(duì)windows環(huán)境,標(biāo)準(zhǔn)c中則linux和windows都可以)

1、GetTickCount()

      調(diào)用函數(shù)需包含windows.h。得到的是系統(tǒng)運(yùn)行的時(shí)間 精確到毫秒,測(cè)試程序如下:

  1. #include <iostream>  
  2. #include <windows.h>   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     double start = GetTickCount();  
  7.     Sleep(1000);  
  8.     double  end=GetTickCount();  
  9.     cout << "GetTickCount:" << end-start << endl;  
  10.         return 0;  
  11. }  

2、GetLocalTime()

      獲得的是結(jié)構(gòu)體保存的year,month等信息。而C語(yǔ)言time函數(shù)獲得是從1970年1月1日0時(shí)0分0秒到此時(shí)的秒數(shù)。需要gmtime函數(shù)轉(zhuǎn)換為常用的日歷(返回的是世界時(shí)間,要顯示常用的時(shí)間,則為localtime函數(shù))。

     在c語(yǔ)言中,保存常用日歷的結(jié)構(gòu)體為struct tm,包含在time.h中,c++語(yǔ)言為SYSTEMTIME結(jié)構(gòu)體,包含在winbase.h(編程包含windows.h即可)。當(dāng)然,精度肯定為秒了。

測(cè)試程序如下:

  1. #include <iostream>  
  2. #include <windows.h>   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     SYSTEMTIME start; //windows.h中   
  7.     GetLocalTime(&start);//time.h的tm結(jié)構(gòu)體一樣的效果   
  8.     cout<< start.year << endl;  
  9. }  
 

c語(yǔ)言的gmtime方法的示范代碼如下:

  1. #include <time.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>   
  4. int main()  
  5. {  
  6.     struct tm *tm_ptr;  
  7.     time_t the_time;  
  8.     (void) time(&the_time);  
  9.     tm_ptr = gmtime(&the_time);  
  10.     printf("Raw time is %ld/n", the_time);  
  11.     printf("gmtime gives:/n");  
  12.     printf("date: %02d/%02d/%02d/n",   
  13.         tm_ptr->tm_year, tm_ptr->tm_mon+1, tm_ptr->tm_mday);  
  14.     printf("time: %02d:%02d:%02d/n",  
  15.         tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec);  
  16.     exit(0);  
  17. }  

另外,c語(yǔ)言有類似于GetLocalTime方法的函數(shù)ctime()。

對(duì)localtime(),原型為:struct tm *localtime(const time_t *timep);將測(cè)試程序的gmtime改為localtime,則可以看到輸出的時(shí)間為爭(zhēng)取時(shí)間和日期了。為了更友好的得到時(shí)間和日期,像date那樣輸出,可以用asctime或ctime函數(shù),原型:char  *ctime(const time_t  *timeval);測(cè)試代碼如下:

  1. #include <time.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>   
  4. int main()  
  5. {  
  6.     time_t the_time;  
  7.     time(&the_time);  
  8.     printf("The date is : %s /n" , ctime(&the_time));  
  9.     exit(0);  
  10. }  
 

3、要獲取高精度時(shí)間,可以使用

       BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)獲取系統(tǒng)的計(jì)數(shù)器的頻率

       BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)獲取計(jì)數(shù)器的值

       然后用兩次計(jì)數(shù)器的差除以Frequency就得到時(shí)間。

測(cè)試程序如下:

  1. #include <iostream>  
  2. #include <windows.h>   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     LARGE_INTEGER m_nFreq;  
  7.     LARGE_INTEGER m_nBeginTime;  
  8.     LARGE_INTEGER nEndTime;  
  9.     QueryPerformanceFrequency(&m_nFreq); // 獲取時(shí)鐘周期   
  10.     QueryPerformanceCounter(&m_nBeginTime); // 獲取時(shí)鐘計(jì)數(shù)   
  11.     Sleep(100);  
  12.     QueryPerformanceCounter(&nEndTime);  
  13.     cout << (double)(nEndTime.QuadPart-m_nBeginTime.QuadPart)*1000/m_nFreq.QuadPart << endl;  
  14. }  
 

需要注意的就是結(jié)果需要強(qiáng)制轉(zhuǎn)換為double,不然會(huì)得到如下錯(cuò)誤:<< is ambiguous。

4、timeGetTime()。

     精度:毫秒,與GetTickCount()相當(dāng)。使用需要包含windows.h,并加入Winmm.lib(雖然查到資料說(shuō)需要包含mmsystem.h,不過(guò)經(jīng)驗(yàn)證,可以不用包含)。測(cè)試代碼如下:

  1. #include <iostream>  
  2. #include <windows.h>//GetTickCount   
  3. //#include <mmsystem.h>   
  4. using namespace std;  
  5. int main()  
  6. {  
  7.     DWORD  start = timeGetTime();//   
  8.     Sleep(1000);  
  9.     DWORD  end= timeGetTime();//   
  10.     cout <<  timeGetTime() << endl;  
  11.     return 0;  
  12. }  

 5、MFC中,CTime::GetCurrentTime() 精確到秒,不列出測(cè)試代碼。

 

關(guān)于定時(shí)器什么的,目前用到地方不多,就不總結(jié)了   

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多