zoukankan      html  css  js  c++  java
  • struct timeval和gettimeofday

    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	%d
    ", 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
    ", 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 
    ",(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
    ",(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;
    }
    
  • 相关阅读:
    python学习笔记[3]-邮件的发送
    python学习笔记[2]-join 和 split
    python学习笔记[1]-凯撒密码
    tar 指令详解
    mysql主从数据库配置
    国产免费高性能桌面录屏软件LiveView1.0正式发布
    原来163的大容量异步上传是这么实现的
    C++ string warning C4251
    你不知道的代码规范---摘自《软件随想录》
    ffmpeg avcodec_decode_video2 解码失败
  • 原文地址:https://www.cnblogs.com/muahao/p/7478207.html
Copyright © 2011-2022 走看看