struct timeval和gettimeofday()

struct timeval结构体在time.h中的定义为:

  1. struct timeval { time_t tv_sec; /* Seconds. */ suseconds_t tv_usec; /* Microseconds. */ };

其中,tv_sec为Epoch到创建struct timeval时的秒数,tv_usec为微秒

struct timeval结构体在time.h中的定义为:

  1. struct timeval
  2. {
  3. time_t tv_sec; /* Seconds. */
  4. suseconds_t tv_usec; /* Microseconds. */
  5. };

其中,tv_sec为Epoch到创建struct timeval时的秒数,tv_usec为微秒数,即秒后面的零头。比如当前我写博文时的tv_sec为1244770435,tv_usec为442388,即当前时间距Epoch时间1244770435秒,442388微秒。需要注意的是,因为循环过程,新建结构体变量等过程需消耗部分时间,我们作下面的运算时会得到如下结果:

  1. int i;
  2. for (i = 0; i < 4; ++i)
  3. {
  4. gettimeofday(&tv, NULL);
  5. printf("%d\t%d\n", tv.tv_usec, tv.tv_sec);
  6. sleep(1);
  7. }
  8. 442388 1244770435
  9. 443119 1244770436
  10. 443543 1244770437
  11. 444153 1244770438

前面为微秒数,后面为秒数,可以看出,在这个简单运算中,只能精确到小数点后面一到两位,或者可以看出,每进行一次循环,均需花费0.005秒的时间,用这个程序来作计时器显然是不行的,除非精确计算产生的代码消耗时间。

gettimeofday() -- 获取当前时间(保存在结构体timeval中)

  1. #include <stdio.h>
  2. #include <sys/time.h>
  3. #include <time.h>
  4. int main(int argc, char * argv[]){
  5. struct timeval tv; //(1)
  6. while(1){
  7. gettimeofday(&tv, NULL); //(2)
  8. printf("time %u:%u\n", tv.tv_sec, tv.tv_usec);
  9. sleep(2);
  10. }
  11. return 0;
  12. }

(1) struct--timeval

  1. struct timeval {
  2. time_t tv_sec; /* seconds */
  3. suseconds_t tv_usec; /* microseconds */
  4. };
  5. millisecond 毫秒
  6. microsecond 微秒
  7. timeval表示一个时间点,比如:
  8. timeval.tv_sec = 1 (s)
  9. timevat.tv_usec = 500 000 s)
  10. 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

  1. #include <sys/time.h>
  2. #include <stdio.h>
  3. int main(void)
  4. {
  5. struct timeval before,after;
  6. long us_t = 100; //us
  7. long max=1000000;
  8. max = max + us_t;
  9. while(1){
  10. gettimeofday(&before,NULL);
  11. sleep(1);
  12. gettimeofday(&after,NULL);
  13. if(((after.tv_sec*1000000 + after.tv_usec ) - (before.tv_sec*1000000 + before.tv_usec)) > max){
  14. 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);
  15. break;
  16. }else{
  17. 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)));
  18. }
  19. }
  20. return 0;
  21. }

struct timeval和gettimeofday的更多相关文章

  1. struct timeval和gettimeofday()

    http://www.cppblog.com/lynch/archive/2011/08/05/152520.html struct timeval结构体在time.h中的定义为: struct ti ...

  2. gettimeofday(struct timeval *tv, struct timezone *tz)函数

    gettimeofday(struct timeval *tv, struct timezone *tz)函数 功能:获取当前精确时间(Unix时间) 其中: timeval为时间 truct tim ...

  3. struct timespec 和 struct timeval

    time()提供了秒级的精确度 . 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在 ...

  4. linux高精度struct timespec 和 struct timeval

    一.struct timespec 定义: typedef long time_t;#ifndef _TIMESPEC#define _TIMESPECstruct timespec {time_t ...

  5. struct timeval结构体 以及 gettimeofday()函数(转)

    struct timeval结构体 转载地址:http://blog.chinaunix.net/uid-20548989-id-2533161.html 该结构体是Linux系统中定义,struct ...

  6. struct timeval 计时问题

    linux编程中,如果用到计时,可以用struct timeval获取系统时间.struct timeval的函数原型如下: struct timeval { __kernel_time_t tv_s ...

  7. struct timeval 和 struct timespec

    struct timeval { time_t tv_sec; suseconds_t tv_usec; }; 測试代码例如以下: #include <stdio.h> #include ...

  8. [c++]struct timeval

    struct timeval { time_t tv_sec; // seconds long tv_usec; // microseconds }; re 1. struct timespec 和 ...

  9. Linux: Linux C 获取当前系统时间的时间戳(精确到秒、毫秒、微秒) gettimeofday

    说明 获取当前的时间的秒数和微秒数本方法需要用到 gettimeofday() 函数,该函数需要引入的头文件是  <sys/time.h>  . 函数说明 int gettimeofday ...

随机推荐

  1. LightOJ1234 Harmonic Number

    /* LightOJ1234 Harmonic Number http://lightoj.com/login_main.php?url=volume_showproblem.php?problem= ...

  2. 洛谷—— P2014 选课

    https://www.luogu.org/problem/show?pid=2014 题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课 ...

  3. Android获取设备屏幕宽高像素值的两个方法

    private void get1() { Resources resources = this.getResources(); DisplayMetrics dm = resources.getDi ...

  4. Vultr好server不敢独享

    Vultr是一家美国2014年成立的新公司.瞬间红遍世界,他是干什么的?他是serverVPS(Virtual Private Server)提供商,这个价格真实惊人的廉价5美金/月.折合人民币30元 ...

  5. 【c语言】数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字

    题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 比如输入一个长度为9的数组{1,2.3.2,2.2.5,4.2}, 因为数组中数字2出现了5次,超过数组的长度的一半,因此输出2 ...

  6. thymeleaf 引入js css 无效

    转自:https://blog.csdn.net/qq_33833327/article/details/81388502

  7. Python3爬虫--两种方法(requests(urllib)和BeautifulSoup)爬取网站pdf

    1.任务简介 本次任务是爬取IJCAI(国际人工智能联合会议)最新2018年的pdf论文文件. 本次编码用到了正则表达式从html里面提取信息,如下对正则表达式匹配规则作简要的介绍. 2.正则表达式规 ...

  8. 2017ACM/ICPC亚洲区沈阳站 C Hdu-6219 Empty Convex Polygons 计算几何 最大空凸包

    题面 题意:给你一堆点,求一个最大面积的空凸包,里面没有点. 题解:红书板子,照抄完事,因为题目给的都是整点,所以最后答案一定是.5或者.0结尾,不用对答案多做处理 #include<bits/ ...

  9. Wannafly挑战赛25 C 期望操作数 数学

    题目 题意:给你你一个数x和一个数q,x<=q,每一次可以等概率把x变成[x,q]中任意一个数,问变成q的步数的期望,输出对998244353取模,多组询问 题解:首先肯定的是,可以预处理,因为 ...

  10. Mysql外键的变种 三种关系

    一.介绍 因为有foreign key的约束,使得两张表形成了三种了关系: 多对一 多对多 一对一 二.重点理解如果找出两张表之间的关系 分析步骤: #1.先站在左表的角度去找 是否左表的多条记录可以 ...