2014-06-26 32 views
1

我正在嘗試使用clock_gettime()來監視已用時間。但是它返回的結果很糟糕。clock_gettime()返回錯誤結果(Debian在VirtualBox上喘息)

我測試了以下內容:

#include <time.h> 
#include <iostream> 
#include <math.h> 

using namespace std; 

int main() 
{ 
    // Time vars for calculation. 
    int ns; 

    // Initial struct. 
    timespec tt; 

    // Get starting time. 
    clock_gettime(CLOCK_MONOTONIC,&tt); 
    int ns_start = tt.tv_nsec; 
    int s_start = tt.tv_sec; 

    // Base for second wrap around. 
    int ns_base = 1000e6 - ns_start; 

    while(true) 
    { 
     cin.ignore(); 

     // Get time. 
     clock_gettime(CLOCK_MONOTONIC,&tt); 

     // Implement/calculate wrap around. 
     if(tt.tv_nsec >= ns_start) ns = tt.tv_nsec - ns_start; 
     else ns = tt.tv_nsec + ns_base; 

     // Display result. 
     cout << "Time Passed:\ts: " << tt.tv_sec-s_start << " ms: " << round(ns/1e6) << endl; 
    } 

    return 0; 
} 

當我握一會兒任意鍵,我得到了類似的結果:

Time Passed: s: 1 ms: 833 



Time Passed: s: 2 ms: 308 
Time Passed: s: 2 ms: 354 
Time Passed: s: 2 ms: 415 


Time Passed: s: 2 ms: 459 
Time Passed: s: 2 ms: 511 


Time Passed: s: 2 ms: 566 
Time Passed: s: 2 ms: 613 


Time Passed: s: 2 ms: 661 
Time Passed: s: 2 ms: 712 


Time Passed: s: 2 ms: 762 
Time Passed: s: 2 ms: 813 


Time Passed: s: 2 ms: 861 
Time Passed: s: 2 ms: 920 // crap starts here 


Time Passed: s: 3 ms: 970 
Time Passed: s: 3 ms: 20 


Time Passed: s: 3 ms: 69 
Time Passed: s: 3 ms: 124 


Time Passed: s: 3 ms: 171 
Time Passed: s: 3 ms: 226 


Time Passed: s: 3 ms: 272 
Time Passed: s: 3 ms: 329 


Time Passed: s: 3 ms: 372 
Time Passed: s: 3 ms: 429 


Time Passed: s: 3 ms: 474 
Time Passed: s: 3 ms: 528 


Time Passed: s: 3 ms: 576 
Time Passed: s: 3 ms: 632 


Time Passed: s: 3 ms: 679 
Time Passed: s: 3 ms: 736 


Time Passed: s: 3 ms: 782 
Time Passed: s: 3 ms: 835 


Time Passed: s: 3 ms: 880 
Time Passed: s: 4 ms: 939 


Time Passed: s: 4 ms: 982 
Time Passed: s: 4 ms: 38 


Time Passed: s: 4 ms: 84 
Time Passed: s: 4 ms: 143 


Time Passed: s: 4 ms: 188 
Time Passed: s: 4 ms: 244 


Time Passed: s: 4 ms: 291 
Time Passed: s: 4 ms: 348 


Time Passed: s: 4 ms: 391 
Time Passed: s: 4 ms: 448 


Time Passed: s: 4 ms: 493 
Time Passed: s: 4 ms: 549 


Time Passed: s: 4 ms: 594 
Time Passed: s: 4 ms: 650 

Time Passed: s: 4 ms: 696 

Time Passed: s: 6 ms: 259 

Time Passed: s: 7 ms: 989 

應該通過查看產生的數字明顯在評論的角度都搞砸了。

任何人有任何想法,爲什麼這是,以及如何解決它?

+0

使用std ::計時 –

+0

我沒有看到這種行爲,但tv_sec和納秒通常渴望。也許你在分配給int時截斷? – Duck

+0

@Duck因爲它是典型的32位程序,所以兩種類型的實現都使用4個字節,所以沒有這種情況發生。 –

回答

1

想象一下,計時器從1.999秒開始。在2.001秒時,你的代碼會說1秒和2毫秒已經過去,實際上它應該是零秒和2毫秒。這是因爲即使納秒部分沒有通過其起始值,您也會從當前秒減去起始秒。

你對納秒環繞有正確的想法。讓我們延長這一點,以保持秒數超過正確的值。下面是做這件事:

#include <time.h> 
#include <iostream> 
#include <math.h> 

using namespace std; 

int main() 
{ 
    // Time vars for calculation. 
    int ns; 
    int s; 

    // Initial struct. 
    timespec tt; 

    // Get starting time. 
    clock_gettime(CLOCK_MONOTONIC,&tt); 
    int ns_start = tt.tv_nsec; 
    int s_start = tt.tv_sec; 

    // Base for second wrap around. 
    int ns_base = 1000e6 - ns_start; 

    while(true) 
    { 
     cin.ignore(); 

     // Get time. 
     clock_gettime(CLOCK_MONOTONIC,&tt); 

     // Implement/calculate wrap around. 
     if(tt.tv_nsec >= ns_start) 
     { 
      ns = tt.tv_nsec - ns_start; 
      s = tt.tv_sec - s_start; 
     } 
     else 
     { 
      ns = tt.tv_nsec + ns_base; 
      s = tt.tv_sec - s_start - 1; 
     } 

     // Display result. 
     cout << "Time Passed:\ts: " << s << " ms: " << round(ns/1e6) << endl; 
    } 

    return 0; 
} 
+0

您好,先生,真棒。進來,分析了問題,提供了最小的(並且與原始代碼的偏差最小),清晰明確地解釋瞭解決方案(帶有根本原因推理),並讓所有人都看起來像鴨子。這是一個真正的專業人員應該如何。謝謝您的回答。 –