我正在嘗試使用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
應該通過查看產生的數字明顯在評論的角度都搞砸了。
任何人有任何想法,爲什麼這是,以及如何解決它?
使用std ::計時 –
我沒有看到這種行爲,但tv_sec和納秒通常渴望。也許你在分配給int時截斷? – Duck
@Duck因爲它是典型的32位程序,所以兩種類型的實現都使用4個字節,所以沒有這種情況發生。 –