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的更多相关文章

  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. [bzoj2049][Sdoi2008]Cave 洞穴勘测_LCT

    Cave 洞穴勘测 bzoj-2049 Sdoi-2008 题目大意:维护一个数据结构,支持森林中加边,删边,求两点连通性.n个点,m个操作. 注释:$1\le n\le 10^4$,$1\le m\ ...

  2. 一个表空间使用率查询sql的优化

    话不多说,直接上运行计划: SQL> set lines 500; SQL> set pagesize 9999; SQL> set long 9999; SQL> selec ...

  3. quick-cocos2d-x游戏开发【9】——单点触摸

    quick的触摸机制,我想廖大已经在这篇文章里说的非常清楚了.我们这些小辈们就是在他的基础上完备一下,说说使用方法就能够了.嘿嘿. 在2.2.3之前的版本号(不包含2.2.3).触摸机制和廖大在那篇文 ...

  4. DIV响应式

    @media only screen and (min-width: 100px) and (max-width: 640px) { div { width: 100px; height: 100px ...

  5. UVa 11466 - Largest Prime Divisor

    題目:給你一個整數n(不超過14位).求出他的最大的素數因子.假设仅仅有一個素數因子輸出-1. 分析:數論. 直接打表計算10^7內的全部素數因子,然後用短除法除n.記錄最大的因子就可以. 假设最後下 ...

  6. ORA-27301: OS failure message: Not enough space

    OS:HP-UNIX ORA-27300: OS system dependent operation:fork failed with status: 12  ORA-27301: OS failu ...

  7. 高可用技术工具包 High Availability Toolkit

    HighAvailabilityToolkit High Availability Toolkit includes several solutions by which achieving arch ...

  8. 网络 - TCP/IP四层模型,面向生产

    TCP.IP四层模型 (网络接口层,网际互联层,传输层,应用层.) (左边是车模,右边是实际生产的车.) 物理层,负责0101比特流传递. 数据链路层,MAC地址负责局域网,内网通信.MAC地址,由4 ...

  9. [PHP] Ecstore 开发学习资源

    一.ECOS框架图 二.学习资源链接 PHP之窗 http://www.phpwindow.com/ ECOS百科大全 http://www.ec-os.net/doc.html

  10. [Plugin] 文件上传利器SWFUpload使用指南

    SWFUpload是 一个flash和js相结合而成的文件上传插件,其功能非常强大.以前在项目中用过几次,但它的配置参数太多了,用过后就忘记怎么用了,到以后要用时又得 到官网上看它的文档,真是太烦了. ...