我如何標記t1和t2兩次並獲得C中的毫秒差值?C編程語言中的時間戳
回答
這會給你的時間,以秒+微秒
#include <sys/time.h>
struct timeval tv;
gettimeofday(&tv,NULL);
tv.tv_sec // seconds
tv.tv_usec // microseconds
使用@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;
}
如果你想找到經過的時間,這種方法將工作只要你不在開始和結束之間重新啓動計算機。
在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中這樣做。
標準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;
類型的精度是實現定義,即日期時間差異可能只返回完整的秒。
還意識到clock()和usleep()之間的交互。 usleep()暫停程序,clock()只測量程序運行的時間。
提到here
/*
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(<ime);
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秒
而不是格式化數據,可以使用asctime(tm),它返回一個字符串。 – 2012-05-04 22:17:10
這個怎麼解決呢?在我的搜索中我沒有看到類似的東西。我試圖避免分裂並使解決方案更簡單。
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);
- 1. C語言編程IAR-時間戳轉換爲可讀格式
- 2. 編程語言C
- 3. C編程語言
- 4. 我的編程語言C#
- 5. Hive查詢語言時間戳
- 6. 前往編程語言 - 系統時間
- 7. RPC編程語言C
- 8. CGI C語言編程
- 9. C語言編程指南
- 10. Shell中的C編程語言
- 11. D編程語言中的C#事件
- 12. 由K&R編寫的C語言編程語言示例CH1
- 13. C程序中的NTP時間戳
- 14. 系統編程語言和應用程序編程語言之間的區別
- 15. strcat()在c語言編程中
- 16. 在C++編程語言中綁定語言
- 17. 從彙編語言編寫C程序
- 18. C#中的時間戳
- 19. C中的時間戳。
- 20. 編程語言
- 21. Go語言time.Parse()用於沒有時區的時間戳
- 22. C語言中:在編譯
- 23. 編程語言的原語
- 24. C語言和多線程編程
- 25. 用於c編程語言的線程的計時器
- 26. 同步線程C語言中的時間測量
- 27. 用Linux編程C語言的參數
- 28. 編寫的編程語言
- 29. 時間戳的語法
- 30. 編譯io編程語言時出錯
單憑標準C無法做到這一點。您可能的解決方案將有所不同,Linux,Windows,手機,收款機,...,微波爐,... – pmg 2009-09-18 13:26:21
@Christoph:time()和clock()都可以返回-1(轉換爲適當的類型)來表示函數在該實現中不可用...和time()(如果可用)的分辨率爲1秒或更差。我細讀了標準版,但沒有發現clock()函數的任何硬性限制 – pmg 2009-09-18 17:58:21
在Linux手冊頁中:「請注意,時間可以環繞。在CLOCKS_PER_SEC等於1000000的32位系統上,此函數將近似返回相同的值每72分鐘一班。「爲了獲得CPU時間getrusage()更好,雖然時鐘是ANSI C的一部分,但getrusage/gettimeofday()不是。 – user172818 2009-09-18 18:58:30