當(dāng)前位置:
首頁(yè) >
Linux下获取毫秒级时间差
發(fā)布時(shí)間:2025/4/14
39
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Linux下获取毫秒级时间差
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Linux下獲取毫秒級(jí)時(shí)間差?
使用Linux的gettimeofday函數(shù)可以達(dá)到這個(gè)目的?其中t1=t_start.tv_sec是公元1970年至今的時(shí)間(換算為秒)?
t2=t_start.tv_usec是當(dāng)前秒數(shù)下的微妙數(shù)?
所以將t1*1000+t2/1000可以得到當(dāng)前的毫秒數(shù)?
引用
#include <stdio.h>?
#include <sys/time.h>?
#include <time.h>?
int gettimeofday(struct timeval *tv, struct timezone *tz);?
int main(int argc,char * argv[]){?
struct timeval t_start,t_end;?
long cost_time = 0;?
//get start time?
gettimeofday(&t_start, NULL);?
long start = ((long)t_start.tv_sec)*1000+(long)t_start.tv_usec/1000;?
printf("Start time: %ld ms\n", start);?
sleep(2);?
usleep(5000);//5毫秒
//get end time?
gettimeofday(&t_end, NULL);?
long end = ((long)t_end.tv_sec)*1000+(long)t_end.tv_usec/1000;?
printf("End time: %ld ms\n", end);?
//calculate time slot?
cost_time = end - start;?
printf("Cost time: %ld ms\n", cost_time);?
return 0;?
}?
提供一些相關(guān)的資料:?
http://book.chinaunix.net/special/ebook/addisonWesley/APUE2/0201433079/ch06lev1sec10.html?
http://www.linuxidc.com/Linux/2010-09/28318.htm?
http://blog.sina.com.cn/s/blog_62a9b1bb0100seyz.html?
http://lizzy115.blog.163.com/blog/static/36491958201102505054953/
總結(jié)
以上是生活随笔為你收集整理的Linux下获取毫秒级时间差的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: yum proxy
- 下一篇: Linux下Python基础调试