struct timeval和gettimeofday
struct timeval和gettimeofday()
struct timeval结构体在time.h中的定义为:
struct timeval { time_t tv_sec; /* Seconds. */ suseconds_t tv_usec; /* Microseconds. */ };
其中,tv_sec为Epoch到创建struct timeval时的秒数,tv_usec为微秒
struct timeval结构体在time.h中的定义为:
struct timeval
{
time_t tv_sec; /* Seconds. */
suseconds_t tv_usec; /* Microseconds. */
};
其中,tv_sec为Epoch到创建struct timeval时的秒数,tv_usec为微秒数,即秒后面的零头。比如当前我写博文时的tv_sec为1244770435,tv_usec为442388,即当前时间距Epoch时间1244770435秒,442388微秒。需要注意的是,因为循环过程,新建结构体变量等过程需消耗部分时间,我们作下面的运算时会得到如下结果:
int i;
for (i = 0; i < 4; ++i)
{
gettimeofday(&tv, NULL);
printf("%d\t%d\n", tv.tv_usec, tv.tv_sec);
sleep(1);
}
442388 1244770435
443119 1244770436
443543 1244770437
444153 1244770438
前面为微秒数,后面为秒数,可以看出,在这个简单运算中,只能精确到小数点后面一到两位,或者可以看出,每进行一次循环,均需花费0.005秒的时间,用这个程序来作计时器显然是不行的,除非精确计算产生的代码消耗时间。
gettimeofday() -- 获取当前时间(保存在结构体timeval中)
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
int main(int argc, char * argv[]){
struct timeval tv; //(1)
while(1){
gettimeofday(&tv, NULL); //(2)
printf("time %u:%u\n", tv.tv_sec, tv.tv_usec);
sleep(2);
}
return 0;
}
(1) struct--timeval
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
millisecond 毫秒
microsecond 微秒
timeval表示一个时间点,比如:
timeval.tv_sec = 1 (s)
timevat.tv_usec = 500 000 (μs)
1:500 = 1s500000μs = 1.5s
(2) gettimeofday()
int gettimeofday(struct timeval *tv, struct timezone *tz);
The functions gettimeofday() and settimeofday() can get and set the time as well as a timezone.
The use of the timezone structure is obsolete; the tz argument should normally be specified as NULL.
(3) 运行结果:
time 1181788367:991487
time 1181788369:991602
表示睡眠2秒经过的精确时间为: 2s115μs
#include <sys/time.h>
#include <stdio.h>
int main(void)
{
struct timeval before,after;
long us_t = 100; //us
long max=1000000;
max = max + us_t;
while(1){
gettimeofday(&before,NULL);
sleep(1);
gettimeofday(&after,NULL);
if(((after.tv_sec*1000000 + after.tv_usec ) - (before.tv_sec*1000000 + before.tv_usec)) > max){
printf("before us:%ld,after us:%ld,value=%ldus > %ld us \n",(before.tv_sec+before.tv_usec),(after.tv_sec+after.tv_usec),((after.tv_sec+after.tv_usec) - (before.tv_sec+before.tv_usec)), us_t);
break;
}else{
printf("before us:%ld,after us:%ld,value=%ldus\n",(before.tv_sec+before.tv_usec),(after.tv_sec+after.tv_usec),((after.tv_sec+after.tv_usec) - (before.tv_sec+before.tv_usec)));
}
}
return 0;
}
struct timeval和gettimeofday的更多相关文章
- struct timeval和gettimeofday()
http://www.cppblog.com/lynch/archive/2011/08/05/152520.html struct timeval结构体在time.h中的定义为: struct ti ...
- gettimeofday(struct timeval *tv, struct timezone *tz)函数
gettimeofday(struct timeval *tv, struct timezone *tz)函数 功能:获取当前精确时间(Unix时间) 其中: timeval为时间 truct tim ...
- struct timespec 和 struct timeval
time()提供了秒级的精确度 . 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在 ...
- linux高精度struct timespec 和 struct timeval
一.struct timespec 定义: typedef long time_t;#ifndef _TIMESPEC#define _TIMESPECstruct timespec {time_t ...
- struct timeval结构体 以及 gettimeofday()函数(转)
struct timeval结构体 转载地址:http://blog.chinaunix.net/uid-20548989-id-2533161.html 该结构体是Linux系统中定义,struct ...
- struct timeval 计时问题
linux编程中,如果用到计时,可以用struct timeval获取系统时间.struct timeval的函数原型如下: struct timeval { __kernel_time_t tv_s ...
- struct timeval 和 struct timespec
struct timeval { time_t tv_sec; suseconds_t tv_usec; }; 測试代码例如以下: #include <stdio.h> #include ...
- [c++]struct timeval
struct timeval { time_t tv_sec; // seconds long tv_usec; // microseconds }; re 1. struct timespec 和 ...
- Linux: Linux C 获取当前系统时间的时间戳(精确到秒、毫秒、微秒) gettimeofday
说明 获取当前的时间的秒数和微秒数本方法需要用到 gettimeofday() 函数,该函数需要引入的头文件是 <sys/time.h> . 函数说明 int gettimeofday ...
随机推荐
- Linux命令及全称(部分)
man: manual 意思是手册,可以用这个命令查询其他命令的用法. pwd:print working directory 显示当前工作路径. su:swith user 切换用户,切换 ...
- Locally managed (LMT) vs. Dictionary managed (DMT) tablespace
The LMT is implemented by adding the extent management local clause to the tablespace definition syn ...
- JavaScript向window onload添加加载函数
有时候我们需要在页面加载事件后完成一些函数操作,对于函数比较多的情况下可以写一个统一的加载函数 .本函数来自于JavaScript DOM 编程艺术: function addLoadEvent(fu ...
- caffe中ConvolutionLayer的前向和反向传播解析及源码阅读
一.前向传播 在caffe中,卷积层做卷积的过程被转化成了由卷积核的参数组成的权重矩阵weights(简记为W)和feature map中的元素组成的输入矩阵(简记为Cin)的矩阵乘积W * Cin. ...
- win7/WIN8.1(x64) 下使用MSDE WIN10不行
通过强制安装(使用管理员权限),手工启动服务的方式,能够在其win7 win81上安装并使用MSDE Microsoft SQL Server 2000 Service Pack 4 Desktop ...
- Java Collection框架—List\ set \map 的异同世界
Java集合是多个对象的容方法.集合(容方法).简单点,事实上就是一个对象,能将具有同样性质的多个元素汇聚成一个总体. Collections Framwork是用来表现和操纵集合的一个统一的体系结构 ...
- iOS快速打企业包ipa
简 首页 专题 发钱啦 注册 登录 简首页专题下载手机应用 gege 2016.01.19 16:55 写了24604字,被92人关注,获得了152个喜欢 iOS快速打企业包ipa 字数256 阅读1 ...
- hadoop权威指南(第四版)要点翻译(5)——Chapter 3. The HDFS(5)
5) The Java Interface a) Reading Data from a Hadoop URL. 使用hadoop URL来读取数据 b) Although we focus main ...
- SQLServer 多点及时备份技巧
为了保证数据库的安全性,我们都会规划数据库的容灾策略,包含本地备份.异地备份.raid.或者使用高可用性(如 日志传送.镜像.复制等)进行异地容灾.因为 SqlServer 数据库的备份仅仅有一个备份 ...
- Makefileeasy犯错的语法
1.引言 近期学习android的Build系统,接触最多的自然就是Makefile语法.发现非常多easy出错的地方,不避开这些错误语法没法真正了解Makefile的内涵.以下就介绍遇到的一些让人困 ...