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

timeval结构体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微秒。需要注意的是,因为循环过程,新建结构体变量等过程需消耗部分时间,我们作下面的运算时会得到如下结果:
#include <sys/time.h>

#include <stdio.h>

int

main(void)

{

    int i;struct timeval tv;for(i = 0; i < 4; i++){gettimeofday(&tv, NULL);printf("%d\t%d\n", tv.tv_usec, tv.tv_sec);sleep(1);}return 0;

}329612 1314851429
329782 1314851430
329911 1314851431
330036 1314851432前面为微秒数,后面为秒数,可以看出,在这个简单运算中,只能精确到小数点后面一到两位,或者可以看出,每进行一次循环,均需花费0.005秒的时间,用这个程序来作计时器显然是不行的,除非精确计算产生的代码消耗时间。
二、gettimeofday()函数原型:/* Get the current time of day and timezone information,

putting it into *TV and *TZ. If TZ is NULL, *TZ is not filled.

Returns 0 on success, -1 on errors.

NOTE: This form of timezone information is obsolete.

Use the functions and variables declared in <time.h> instead. */

extern int gettimeofday (struct timeval *__restrict __tv,

                     __timezone_ptr_t __tz) __THROW __nonnull ((1));gettimeofday()功能是得到当前时间和时区,分别写到tv和tz中,如果tz为NULL则不向tz写入。

struct timeval相关推荐

  1. 结构体struct timeval 和 struct timespec的定义

    结构体struct timeval 和 struct timespec的定义均在头文件<sys/time.h>中,具体定义如下: struct timeval {long tv_sec; ...

  2. struct timeval结构体

    struct timeval结构体在time.h中的定义为: struct timeval { __time_t tv_sec;        /* Seconds. */ __suseconds_t ...

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

    一.struct timeval结构体 struct timeval结构体在time.h中的定义为: struct timeval { __time_t tv_sec;        /* Secon ...

  4. struct timeval用法与时间溢出问题

    1.用法 高精度计算时间的结构体struct timeval. 头文件是sys/time.h 网上查找的定义是 #include "sys/time.h"struct timeva ...

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

    struct timeval结构体 struct timeval结构体在time.h中的定义为: struct timeval { __time_t tv_sec; /* Seconds. */ __ ...

  6. linux系统中struct timeval结构体、struct timezone结构体以及gettimeofday函数

    格林尼治时间.协调世界时 间.世界时间.日光节约时间以及时区等介绍: 格林尼治时间(Greenwich Mean Time,GMT)是指位于英国伦敦郊区的皇家格林尼治天文台当地的标准时间,因为本初子午 ...

  7. Linux时间相关知识小结:struct timeval、timespec、gettimeofday、time、localtime....

    前言 我们在linux平台进行开发时,时间相关的操作基本上都会遇到,本文就对常用的时间相关的结构体.接口进行分析小结. 常见类型.结构体定义 timespec 原型 struct timespec { ...

  8. linux timeval,Linux上关于C语言的struct timeval{}的一道题

    Linux下关于C语言的struct timeval{}的一道题 结构定义中有: struct timeval{ long tv_sec; long tv_usec; }; 设计一个程序:计算机屏幕呈 ...

  9. timeval linux 头文件,linux中的struct timeval结构

    struct timeval { long tv_sec; // seconds long tv_usec; // and microseconds(百万分之一秒) }; timeval结构的用于指定 ...

最新文章

  1. 比特币多重签名机制使用篇
  2. 算法导论之贪心算法(Huffman编码和拟阵)
  3. “数据中心运维管理VIP学习群”问题汇总(一)
  4. 【PM模块】维护处理简介
  5. MyBatis快速入门及深入
  6. 面向睡眠阶段分类的自适应时空图卷积神经网络
  7. 马逊s3云存储接口_使用 Amazon S3 云服务轻松实现存储
  8. Galgame研发日志:独立游戏制作前应当进行的第一步
  9. flowable springboot 集成 flowableDMN_05
  10. python支付程序源码_支付宝推出新活动,Python脚本能让你赚的更多!(附源码)...
  11. 小米相机曝光_小米11pro曝光,超级屏+双6400万+骁龙875,不愧是小米旗舰
  12. android 混淆打包教程,Android 混淆打包
  13. python字符串引用包_如何通过字符串形式导包(importlib模块的使用)
  14. 产品经理——跨境电商订单跟进邮件模板
  15. 计算机科学教学指导委员会,教育部高等学校计算机专业教学指导委员会委员 徐久成教授...
  16. linux or msys2设置网络代理
  17. 古墓丽影10linux,《古墓丽影11:暗影》Linux平台与Windows平台流畅度对比
  18. 无线耳机除了苹果哪个牌子好?类似苹果耳机的蓝牙耳机推荐
  19. Linux命令速查---kalrry
  20. 顺丰快递:请签收MySQL灵魂十连

热门文章

  1. 开源正当时,共赢新未来 OpenHarmony开发者大会成功召开
  2. 导航系统中的惯性技术
  3. Fluent API
  4. where online services go when they dieMichael Doi
  5. 在行内样式 或 对象格式的样式中 使用scss变量
  6. CISC(复杂指令集)与RISC(精简指令集)的区别
  7. Roadblocks(次短路)
  8. SqlServer数据库应用
  9. AVL树添加节点后的平衡操作(一)逻辑分析:左旋、右旋、双旋(超详细图解)
  10. px和分辨率的关系总结