• 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

  • 相关阅读:
    矩阵快速幂模板
    POJ 3761 Bubble Sort 快速幂取模+组合数学
    MySQL批量修改表前缀
    js生成条形码插件
    如何将本地代码通过git上传到码云
    jQuery常用方法
    MySQL按日、周、月统计数据
    PHP文件下载
    python报错ModelNotFoundError
    thinkphp生成的验证码提示因存在错误无法显示
  • 原文地址:https://www.cnblogs.com/eustoma/p/2475413.html
Copyright © 2020-2023  润新知