Linux下获取毫秒级时间差
来源:互联网 发布:java后端调用url请求 编辑:程序博客网 时间:2024/06/07 01:52
Linux下获取毫秒级时间差
使用Linux的gettimeofday函数可以达到这个目的
其中t1=t_start.tv_sec是公元1970年至今的时间(换算为秒)
t2=t_start.tv_usec是当前秒数下的微妙数
所以将t1*1000+t2/1000可以得到当前的毫秒数
#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;
}
提供一些相关的资料:
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/
其中t1=t_start.tv_sec是公元1970年至今的时间(换算为秒)
t2=t_start.tv_usec是当前秒数下的微妙数
所以将t1*1000+t2/1000可以得到当前的毫秒数
引用
#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;
}
提供一些相关的资料:
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/
- Linux下获取毫秒级时间差
- Linux下获取毫秒级时间差
- Linux下获取时间差(毫秒级)
- windows获取时间差毫秒级
- Linux下获取毫秒级时间
- Linux获取毫秒级时间
- Linux获取毫秒级时间
- JSP下获取毫秒级时间戳
- 使用timeval获取Linux 毫秒级计时
- C++获得毫秒级的时间差
- linux 获取时间差
- mfc中,在给定时间的情况下,取得毫秒级的时间差
- object-c获取毫秒值,时间差,计算执行耗费时间
- linux下获取当前时间(精确到毫秒)
- linux(ubuntu)下获取 ms 毫秒级别 时间
- Linux下得到毫秒级时间
- Linux 下 毫秒级延时函数
- linux 通过自己计算时间差的方法来定时 毫秒
- Java代码内存分析工具
- 编程综合—先思考后debug
- structs2中全面的配置文件struts.xml文件示范
- struts标签库:配置和使用
- C# WinIO键盘鼠标模拟
- Linux下获取毫秒级时间差
- Python实现 Linux tree命令:用树形结构显示目录结构
- UIPickerView中的实例方法
- Web.xml配置详解
- linux笔记四(磁盘与文件系统管理)
- 解决开机显示 BOOTMGR is missing 的方法
- java中类型所占字节数以及可表示的数字个数
- hdu4389-X mod f(x)-多校9-1010题解
- 大端小端(Big- Endian和Little-Endian)