c/c++在windows下獲取時間和計算時間差的幾種方法總結 【轉】


http://blog.csdn.net/coder_xia/article/details/6566708

一、標准C和C++都可用

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

測試程序如下:

[c-sharp] view plain copy
  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測試通過。

    關於代碼中的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 plain copy
  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++中(此處針對windows環境,標准c中則linux和windows都可以)

1、GetTickCount()

      調用函數需包含windows.h。得到的是系統運行的時間 精確到毫秒,測試程序如下:

 

[c-sharp] view plain copy
  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()

      獲得的是結構體保存的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 plain copy
  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結構體一樣的效果  
  8.     cout<< start.year << endl;  
  9. }  
 

c語言的gmtime方法的示范代碼如下:

 

[c-sharp] view plain copy
  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語言有類似於GetLocalTime方法的函數ctime()。

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

[c-sharp] view plain copy
  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、要獲取高精度時間,可以使用

       BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)獲取系統的計數器的頻率

       BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)獲取計數器的值

       然后用兩次計數器的差除以Frequency就得到時間。

測試程序如下:

[c-sharp] view plain copy
  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); // 獲取時鍾周期  
  10.     QueryPerformanceCounter(&m_nBeginTime); // 獲取時鍾計數  
  11.     Sleep(100);  
  12.     QueryPerformanceCounter(&nEndTime);  
  13.     cout << (double)(nEndTime.QuadPart-m_nBeginTime.QuadPart)*1000/m_nFreq.QuadPart << endl;  
  14. }  
 

需要注意的就是結果需要強制轉換為double,不然會得到如下錯誤:<< is ambiguous。

4、timeGetTime()。

     精度:毫秒,與GetTickCount()相當。使用需要包含windows.h,並加入Winmm.lib(雖然查到資料說需要包含mmsystem.h,不過經驗證,可以不用包含)。測試代碼如下:

[c-sharp] view plain copy
  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() 精確到秒,不列出測試代碼。

 

關於定時器什么的,目前用到地方不多,就不總結了

 

參考網址:

1、http://blog.csdn.net/wallaceli1981/archive/2009/10/24/4723218.aspx 

2、http://wenku.baidu.com/view/beb3c9eef8c75fbfc77db2b5.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM