如何使用C获取以毫秒为单位的当前时间?
问题描述:
我该如何获得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
没有在它的毫秒成员。
答
- 在Unix上,使用gettimeofday()以毫秒为单位获得答案,并缩放到毫秒。
- 或者使用POSIX clock_gettime()以毫微秒获取答案,并缩放到毫秒。
参见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