Linux獲取毫秒級時間

2021-05-21 21:49:29 字數 1054 閱讀 9934

linux獲取毫秒級時間

moakap

在軟體設計中經常會用到關於時間的處理,用來計算語句、函式的執行時間,這時就需要精確到毫秒甚至是微妙的時間。

int gettimeofday(struct timeval *tv

, struct timezone *tz

);

int settimeofday(const struct timeval *tv

, const struct timezone *tz

);

struct timeval ;

struct timezone ;

… struct timeval t_start,t_end;

long cost_time = 0;

//get start time

gettimeofday(&t_start, null);

printf("start time: %ld us", t_start.tv_usec);

//some operation …

//get end time

gettimeofday(&t_end, null);

printf("end time: %ld us", t_end.tv_usec);

//calculate time slot

cost_time = t_end.tv_usec - t_start.tv_usec;

printf("cost time: %ld us", cost_time); …

輸出:start time: 438061 us

end time: 459867 us

cost time: 21806 us

Linux獲取毫秒級時間

在軟體設計中經常會用到關於時間的處理,用來計算語句 函式的執行時間,這時就需要精確到毫秒甚至是微妙的時間。int gettimeofday struct timeval tv,struct timezone tz int settimeofday const struct timeval tv,co...

Linux下獲取毫秒級時間差

使用linux的gettimeofday函式可以達到這個目的 其中t1 t start.tv sec是公元1970年至今的時間 換算為秒 t2 t start.tv usec是當前秒數下的微妙數 所以將t1 1000 t2 1000可以得到當前的毫秒數 引用 include include incl...

C 時間操作(獲取毫秒級)

使用標準c語言的time函式,可以滿足一般性需要 include include int main void time t t time 0 char tmp 64 strftime tmp,sizeof tmp y m d x a 本年第 j天 z localtime t puts tmp ret...