計算程序運行時間精確到毫秒

#include<sys/timeb.h>
struct timeb t1,t2;
void main(){ 
ftime(&t1);
//code do something 
ftime(&t2); 
printf("Msecond : %ld \n",(t2.time - t1.time)*1000+t2.millitm - t1.millitm );
}
</pre><pre name="code" class="cpp">

struct timeb
{
time_t time; //秒
unsigned short millitm;//毫秒
short timezone;//時區
short dstflag;//時區標
}

timeb.h中聲明瞭timeb結構,ftime(timeb&)獲取當前時間,可以精確到毫秒級別。timeb.millitm表示毫秒
time.h中聲明瞭tm結構體,可以記錄年月日、時分秒,但不支持毫秒級的計時方式,用time(NULL)函數獲取當前時間。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章