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 ...
随机推荐
- Spring MVC-Hello World示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_hello_world_example.htm 说明:示例基于Spring MVC ...
- SPOJ 1771&&DLX精确覆盖,重复覆盖
DLX的题,做过这题才算是会吧. 这道题转化成了精确覆盖模型来做,一开始,只是单纯的要覆盖完行列和斜线,WA. 后来醒悟了,不能这样,只要覆盖全部行或列即可.虽然如此,但某些细节地方很关键不能考虑到. ...
- Struts2 动态结果集
1.index.jsp <body> 动态结果 一定不要忘了为动态结果的保存值设置set get方法 <ol> <li><a href="user/ ...
- 关于JOS 未对全部内存分页映射之前 物理地址映射问题的思考
在kern/pmap.c 里面会又以下这段代码,要知道boot_alloc只会分配线性地址,真正建立虚拟页和物理页映射关系的在后面的page_alloc. ////////////////////// ...
- scala并发编程原生线程Actor、Case Class下的消息传递和偏函数实战
參考代码: import scala.actors._ case class Person(name:String,age:Int) class HelloActor extends Actor{ d ...
- 第14章3节《MonkeyRunner源代码剖析》 HierarchyViewer实现原理-HierarchyViewer实例化
既然要使用HierarchyViewer来获取控件信息,那么首先我们看下在脚本中.我们是怎么获得HierarchyViewer的,看以下一段脚本代码: 1 device = MonkeyRunner. ...
- Linux学习(二) wget命令的使用
近期在Linux下进行一些操作,在非常多地方都用到了wget这个命令,记录一下一些有关wget的使用方法: wget是在Linux下开发的开放源码的软件,作者是Hrvoje Niksic,后来被移植到 ...
- 自定义控件三部曲之动画篇(四)——ValueAnimator基本使用
前言:不要让别人的无知断送了你的梦想,永远坚信你所坚信的. 相关文章: <Android自定义控件三部曲文章索引>:http://blog.csdn.net/harvic880925/ar ...
- modelstate.isvalid false
http://stackoverflow.com/questions/1791570/modelstate-isvalid-false-why 第一个 About "can it be th ...
- P1127 词链
P1127 词链 题目描述 如果单词X的末字母与单词Y的首字母相同,则X与Y可以相连成X.Y.(注意:X.Y之间是英文的句号“.”).例如,单词dog与单词gopher,则dog与gopher可以相连 ...