2009-09-02 128 views
3

我該如何獲得C中的當前時間(以毫秒爲單位)?我做以下,以獲得時間(秒):如何使用C獲取以毫秒爲單位的當前時間?

struct tm ptm; 

now = time(NULL); 

localtime_r(&now,ptm); 

myTime= (ptm->tm_hour * 3600) + (ptm->tm_min * 60) + (ptm->tm_sec); 

在time.h中尋找,struct tm沒有在它的毫秒成員。

+1

參見http://stackoverflow.com/questions/361363/how-to-measure-time-in-milliseconds-using-ansi- c http://stackoverflow.com/questions/173409/how-can-i-find-the-execution-time-of-a-section-of-my-program-in-c – 2009-09-02 22:05:05

回答

5
  • 在Unix上,使用gettimeofday()以毫秒爲單位獲得答案,並縮放到毫秒。
  • 或者使用POSIX clock_gettime()以毫微秒獲取答案,並縮放到毫秒。
相關問題