c++ timeval

struct 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微秒。需要注意的是,因为循环过程,新建结构体变量等过程需消耗部分时间,我们作下面的运算时会得到如下结果:
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秒的时间,用这个程序来作计时器显然是不行的,除非精确计算产生的代码消耗时间。

时间: 2024-07-31 13:34:10

c++ timeval的相关文章

struct timeval 计时问题

linux编程中,如果用到计时,可以用struct timeval获取系统时间.struct timeval的函数原型如下: struct timeval { __kernel_time_t tv_sec; /* seconds */ __kernel_suseconds_t tv_usec; /* microseconds */ }; 比如,如果要计算某代码运行的时间,可以使用如下代码: int main() { struct timeval tv; long long start_time,

C语言通过timeval结构设置周期

在C语言中,我们经常需要设置一个时间周期.在这里,我们通过Timeval结构实现时间周期的设置.首先,我们介绍timeval,其定义如下(转载http://www.cnblogs.com/wainiwann/archive/2012/11/28/2792133.html): "timeval是一个结构体,在time.h中定义为:struct timeval{     __time_t tv_sec;                /* Seconds. */     __suseconds_t

struct timeval 和 struct timespec

struct timeval { time_t tv_sec; suseconds_t tv_usec; }; 测试代码如下: #include <stdio.h> #include <sys/time.h> #include <time.h> int gettimeofday(struct timeval *tv, struct timezone *tz); int main(int argc,char * argv[]){ struct timeval tv; wh

struct timeval和gettimeofday()

http://www.cppblog.com/lynch/archive/2011/08/05/152520.html 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结构体在ti

struct timeval

struct timeval { __time_t tv_sec; /* Seconds. */ __suseconds_t tv_usec; /* Microseconds. */ }; /* 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.

struct timespec 和 struct timeval

time()提供了秒级的精确度 . 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在的秒数 用time()函数结合其他函数(如:localtime.gmtime.asctime.ctime)可以获得当前系统时间或是标准时间. 如果需要更高的时间精确度,就需要struct timespec 和 struct timeval来处理: 一.struct timespec 定义: typedef l

使用未定义的 struct “timeval” 解决方案

场景 在应用VS编译RabbitMQ examples目录下的amqp_comsumer.c文件,出现如上的问题,调用amqp_consume_message函数指定超时时间 struct timeval tvTimeout; tvTimeout.tv_sec = 1; tvTimeout.tv_usec = 0; ret = amqp_consume_message(conn, &envelope, &tvTimeout, 0); 解决 添加头文件#include <windows

timeval的时间转换成毫秒之后多大的数据类型可以装下

struct timeval { long tv_sec; /*秒*/ long tv_usec; /*微秒*/ }; 秒的定义为long,为了防止溢出,转换成毫秒之后保存在long long中 原文地址:https://www.cnblogs.com/AKUN-FYK/p/10981984.html

Linux-hexdump命令调试event驱动—详解(13)

hexdump: 查看文件的内容,比如二进制文件中包含的某些字符串,通常用来调试驱动用 1.调试 键盘驱动 讲解 当我们insmod挂载了键盘驱动后,找到键盘驱动被放在event1设备里, 此时没有按键按下,所以event1里面的数据是没有的,那么数据又是从来哪里来? 通过键盘驱动的read函数,若有按键按下,就会上传按键数据给用户层,此时的用户层就是hexdump 因为键盘驱动的input_handler 是:evdev_handler 所以键盘驱动的read函数是: evdev_handle