struct timeval结构体

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

struct timeval
{
__time_t tv_sec;        /* Seconds. */
__suseconds_t tv_usec;  /* Microseconds. */  //微妙,us, 1s=1000ms=1000000us
};

其中,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.%d\n", tv.tv_sec, tv.tv_usec);sleep(1);}return 0;
}


前面为秒数,小数点后面为微秒数,可以看出,在这个简单运算中,只能精确到小数点后面一到两位,或者可以看出,每进行一次循环,均需花费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写入。TZ是时区,time zone ,TV是时间值,即time value。

struct timespec

typedef long time_t;
#ifndef _TIMESPEC
#define _TIMESPEC
struct timespec {
time_t tv_sec; // seconds
long tv_nsec; // and nanoseconds
};
#endif
struct timespec有两个成员,一个是秒,一个是纳秒, 所以最高精确度是纳秒。
一般由函数int clock_gettime(clockid_t, struct timespec )获取特定时钟的时间,常用如下4种时钟:
CLOCK_REALTIME 统当前时间,从1970年1.1日算起
CLOCK_MONOTONIC 系统的启动时间,不能被设置
CLOCK_PROCESS_CPUTIME_ID 本进程运行时间
CLOCK_THREAD_CPUTIME_ID 本线程运行时间
struct tm localtime(const time_t clock); //线程不安全
struct tm
localtime_r( const time_t
timer, struct tm
result );//线程安全
size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr );

#include<stdio.h>
#include<time.h>
#include<sys/time.h>void nowtime_ns()
{printf("---------------------------struct timespec---------------------------------------\n"); printf("[time(NULL)]     :     %ld\n", time(NULL)); struct timespec ts;clock_gettime(CLOCK_REALTIME, &ts);printf("clock_gettime : tv_sec=%ld, tv_nsec=%ld\n", ts.tv_sec, ts.tv_nsec);struct tm t;char date_time[64];strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&ts.tv_sec, &t));printf("clock_gettime : date_time=%s, tv_nsec=%ld\n", date_time, ts.tv_nsec);
}
void nowtime_us()
{printf("---------------------------struct timeval----------------------------------------\n"); printf("[time(NULL)]    :    %ld\n", time(NULL)); struct timeval us;gettimeofday(&us,NULL);printf("gettimeofday: tv_sec=%ld, tv_usec=%ld\n", us.tv_sec, us.tv_usec);struct tm t;char date_time[64];strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&us.tv_sec, &t));printf("gettimeofday: date_time=%s, tv_usec=%ld\n", date_time, us.tv_usec);
}int main(int argc, char* argv[])
{nowtime_ns();printf("\n");nowtime_us();printf("\n");return 0;
}

运行的结果:
---------------------------struct timespec---------------------------------------
[time(NULL)] : 1400233995
clock_gettime : tv_sec=1400233995, tv_nsec=828222000
clock_gettime : date_time=2014-05-16 17:53:15, tv_nsec=828222000

---------------------------struct timeval----------------------------------------
[time(NULL)] : 1400233995
gettimeofday: tv_sec=1400233995, tv_usec=828342
gettimeofday: date_time=2014-05-16 17:53:15, tv_usec=828342

参考:http://blog.chinaunix.net/uid-20548989-id-2533161.html
https://www.cnblogs.com/book-gary/p/3716790.html

struct timeval结构体 以及 gettimeofday()函数、struct timespec结构体相关推荐

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

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

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

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

  3. c语言结构体调用成员函数,c语言结构体函数调用参数如何设置

    c语言结构体函数调用参数怎么设置 函数结构是下面的代码,main函数中如何调用showinfo函数,参数应该怎么设置,对参数的设置不太明白 C/C++ code#include #define SIZ ...

  4. 对C语言 结构指针变量做函数的参数 结构作为函数的参数

    一.结构指针变量做函数的参数 /* 用指针变量作函数参数进行传送,这时由实参传向形参的只是地址,从而减少了时间和空间的开销. */ /* 计算一组学生的平均成绩和不及格的人数,用结构指针变量作函数参数 ...

  5. C++_结构体指针_嵌套结构体_结构体做为函数参数_结构体值传递和指针传递---C++语言工作笔记026

    然后我们来看结构体指针. 可以看到我们先去定义一个结构体 然后我们在main函数中,去声明一个结构体 s 然后我们定义一个指针 int *p = &s; 指向这个结构体变量. 这里要注意

  6. struct timeval

    struct timeval结构体 以及 gettimeofday()函数 timeval结构体struct timeval结构体在time.h中的定义为: struct timeval { __ti ...

  7. linux中C语言获取高精度时钟gettimeofday函数

    原文地址::https://blog.csdn.net/balingybj/article/details/48293817 相关文章 1.gettimeofday()函数的使用方法----https ...

  8. linux 下的gettimeofday 函数在windows上的替换方案

    方案一: #include <time.h> #ifdef WIN32 #   include <windows.h> #else #   include <sys/ti ...

  9. C语言中关于向函数中传入结构指针的易错点及解决方案

    前言(Introduction): 最近在学习链表的过程中,我写了这样一段函数: Recently in the process of learning the linked-list, I wrot ...

最新文章

  1. kotlin面向对象之接口、代理与委托、单例模式
  2. ADO,OLEDB,ODBC,DAO的区别
  3. Python IDLE 如何清屏
  4. 【C语言】str类与men库函数的实现(如:strcpy,strcmp,strstr,strcat,memmove,memcpy)
  5. 文件夹 计算机无法使用,电脑文件夹提示被使用无法删除怎么办
  6. GetWindowThreadProcessId
  7. FMS案例开发--视频聊天室(三)
  8. 彩色人物创意灵感|C4D万物皆可造!
  9. 动态规划-最大的正方形面积
  10. 专访李运华:程序员如何在技术上提升自己
  11. Kali 安装 xmapp、DVWA
  12. 人工智能认知技术,在各行业的应用介绍
  13. 12306抢票,12306抢票工具神奇插件3个
  14. 广州特耐苏-广州风淋通道构造及特点
  15. 无损放大图片软件有哪些?试试这些图片无损放大工具
  16. Capture One 21 Pro v14.3.0.185 飞思顶级图像后期处理编辑软件
  17. Python中的布尔类型
  18. 一位ORACLE DBA大牛离职时候的过往总结
  19. ①java自学笔记——java基本语法
  20. C语言a++ ++a

热门文章

  1. error LNK2001: 无法解析的外部符号 __imp__WSAGetLastError@0
  2. 通用nodejs正则表达式
  3. fonts/fontawesome-webfont.woff2 404
  4. 2022年国自然正式放榜,如何解读基金立项结果?(附查询方法)
  5. Springboot使用HTML模板发送电子邮件
  6. JAVA毕业设计公立医院绩效考核系统计算机源码+lw文档+系统+调试部署+数据库
  7. 帮我从求职者的角度分析一下现在使用人数最多的十大汉语线上教学平台的申请难度...
  8. 图解 git 仓库概念
  9. 假期错过的...条AI新闻都在这里了
  10. 刷脸支付设备深度融合多项赋能