2009-09-18 140 views
22

我如何標記t1和t2兩次並獲得C中的毫秒差值?C編程語言中的時間戳

+5

單憑標準C無法做到這一點。您可能的解決方案將有所不同,Linux,Windows,手機,收款機,...,微波爐,... – pmg 2009-09-18 13:26:21

+0

@Christoph:time()和clock()都可以返回-1(轉換爲適當的類型)來表示函數在該實現中不可用...和time()(如果可用)的分辨率爲1秒或更差。我細讀了標準版,但沒有發現clock()函數的任何硬性限制 – pmg 2009-09-18 17:58:21

+1

在Linux手冊頁中:「請注意,時間可以環繞。在CLOCKS_PER_SEC等於1000000的32位系統上,此函數將近似返回相同的值每72分鐘一班。「爲了獲得CPU時間getrusage()更好,雖然時鐘是ANSI C的一部分,但getrusage/gettimeofday()不是。 – user172818 2009-09-18 18:58:30

回答

28

這會給你的時間,以秒+微秒

#include <sys/time.h> 
struct timeval tv; 
gettimeofday(&tv,NULL); 
tv.tv_sec // seconds 
tv.tv_usec // microseconds 
0

U可以嘗試在C時間庫(time.h)例程。另外在同一個庫中查看clock()。自編程開始以來,它會給出時鐘滴答聲。但是你可以在你想要專注的操作之前保存它的值,然後在操作之後再次捕獲cliock ticks並找出差異,然後獲得時間差。

3

使用@Arkaitz希門尼斯的代碼,以獲得兩個timevals:

#include <sys/time.h> 
//... 
struct timeval tv1, tv2, diff; 

// get the first time: 
gettimeofday(&tv1, NULL); 

// do whatever it is you want to time 
// ... 

// get the second time: 
gettimeofday(&tv2, NULL); 

// get the difference: 

int result = timeval_subtract(&diff, &tv1, &tv2); 

// the difference is storid in diff now. 

示例代碼timeval_subtract可以在this web site發現:

/* Subtract the `struct timeval' values X and Y, 
    storing the result in RESULT. 
    Return 1 if the difference is negative, otherwise 0. */ 

int 
timeval_subtract (result, x, y) 
     struct timeval *result, *x, *y; 
{ 
    /* Perform the carry for the later subtraction by updating y. */ 
    if (x->tv_usec < y->tv_usec) { 
    int nsec = (y->tv_usec - x->tv_usec)/1000000 + 1; 
    y->tv_usec -= 1000000 * nsec; 
    y->tv_sec += nsec; 
    } 
    if (x->tv_usec - y->tv_usec > 1000000) { 
    int nsec = (x->tv_usec - y->tv_usec)/1000000; 
    y->tv_usec += 1000000 * nsec; 
    y->tv_sec -= nsec; 
    } 

    /* Compute the time remaining to wait. 
     tv_usec is certainly positive. */ 
    result->tv_sec = x->tv_sec - y->tv_sec; 
    result->tv_usec = x->tv_usec - y->tv_usec; 

    /* Return 1 if result is negative. */ 
    return x->tv_sec < y->tv_sec; 
} 
+0

timeval_subtract中的代碼是邪惡的,因爲它修改了輸入值y。如果輸入是兩個結構timeval值 - 這與指針不同,那不會有什麼不好。但是當評估'x - y'時,通常不會期望計算改變存儲在'y'中的值。 – 2009-09-18 13:33:50

+0

@Jonathan,真的。雖然簡單地將其更改爲通過複製實施將解決該問題 – Glen 2009-09-18 13:43:14

+0

我同意。我會修復它,但我不能仔細檢查我的修改是否會在此刻編譯,所以我想我會保持原樣。 – Bill 2009-09-18 13:55:43

6

如果你想找到經過的時間,這種方法將工作只要你不在開始和結束之間重新啓動計算機。

在Windows中,使用GetTickCount()。具體方法如下:現在

DWORD dwStart = GetTickCount(); 
... 
... process you want to measure elapsed time for 
... 
DWORD dwElapsed = GetTickCount() - dwStart; 

dwElapsed是經過的毫秒數。

在Linux中,使用時鐘()CLOCKS_PER_SEC做同樣的事情。

如果您需要持續重啓或跨PC的時間戳(確實需要相當好的syncronization),然後使用其他方法(gettimeofday())。

另外,在Windows中,至少你可以比標準時間分辨率好得多。通常,如果您在緊密循環中調用GetTickCount(),則每次更改時都會看到它跳躍10-50。這是因爲Windows線程調度程序使用的時間量。這或多或少是每個線程在切換到別的之前運行的時間量。在你的程序或進程和年初

timeBeginPeriod(1); 

:如果你做一個

timeEndPeriod(1); 

末,那麼量子將變更爲1毫秒,您將得到更好的時間分辨率GetTickCount()調用。但是,這確實會對整個計算機如何運行進程做出微妙的更改,因此請記住這一點。然而,Windows Media Player和其他許多事情無論如何都是這樣做的,所以我不必太擔心。我敢肯定,在Linux中可能有某種方法可以做到這一點(可能有更好的控制,或者可能有亞毫秒級的量子),但我還沒有必要在Linux中這樣做。

8

標準C99:

#include <time.h> 

time_t t0 = time(0); 
// ... 
time_t t1 = time(0); 
double datetime_diff_ms = difftime(t1, t0) * 1000.; 

clock_t c0 = clock(); 
// ... 
clock_t c1 = clock(); 
double runtime_diff_ms = (c1 - c0) * 1000./CLOCKS_PER_SEC; 

類型的精度是實現定義,即日期時間差異可能只返回完整的秒。

+0

日期時間差最多返回完整秒。如果我正確地解釋了標準,則time()不返回(time_t)-1時,不能保證每秒都返回新值:例如,它可以具有5秒或1分鐘的分辨率。 – pmg 2009-09-18 18:04:37

+2

@pmg:精度是實現定義的,例如在我的系統上'time()'具有'1s'分辨率; 'clock()'的精度通常儘可能高,但它可以測量運行時間而不是日期時間 – Christoph 2009-09-18 18:25:05

1

還意識到clock()和usleep()之間的交互。 usleep()暫停程序,clock()只測量程序運行的時間。

提到here

5
/* 
Returns the current time. 
*/ 

char *time_stamp(){ 

char *timestamp = (char *)malloc(sizeof(char) * 16); 
time_t ltime; 
ltime=time(NULL); 
struct tm *tm; 
tm=localtime(&ltime); 

sprintf(timestamp,"%04d%02d%02d%02d%02d%02d", tm->tm_year+1900, tm->tm_mon, 
    tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); 
return timestamp; 
} 


int main(){ 

printf(" Timestamp: %s\n",time_stamp()); 
return 0; 

} 

輸出如果可能會更好使用的gettimeofday():時間戳:20110912130940 // 2011年09月12十三點09分40秒

+0

而不是格式化數據,可以使用asctime(tm),它返回一個字符串。 – 2012-05-04 22:17:10

2

這個怎麼解決呢?在我的搜索中我沒有看到類似的東西。我試圖避免分裂並使解決方案更簡單。

struct timeval cur_time1, cur_time2, tdiff; 

    gettimeofday(&cur_time1,NULL); 
    sleep(1); 
    gettimeofday(&cur_time2,NULL); 

    tdiff.tv_sec = cur_time2.tv_sec - cur_time1.tv_sec; 
    tdiff.tv_usec = cur_time2.tv_usec + (1000000 - cur_time1.tv_usec); 

    while(tdiff.tv_usec > 1000000) 
    { 
     tdiff.tv_sec++; 
     tdiff.tv_usec -= 1000000; 
     printf("updated tdiff tv_sec:%ld tv_usec:%ld\n",tdiff.tv_sec, tdiff.tv_usec); 
    } 

    printf("end tdiff tv_sec:%ld tv_usec:%ld\n",tdiff.tv_sec, tdiff.tv_usec);