zoukankan      html  css  js  c++  java
  • linux下C语言获取微秒级时间

    使用C语言在linux环境下获得微秒级时间

    1、数据结构

    int gettimeofday(struct timeval*tv, struct timezone *tz);

    其参数tv是保存获取时间结果的结构体,参数tz用于保存时区结果:

    struct timezone{
    int tz_minuteswest;/*格林威治时间往西方的时差*/
    int tz_dsttime;/*DST 时间的修正方式*/
    }

    timezone 参数若不使用则传入NULL即可。

    而结构体timeval的定义为:

    struct timeval{
    long int tv_sec; // 秒数
    long int tv_usec; // 微秒数
    }

    2、代码实例 temp.cpp

    #include <stdio.h>        // for printf()
    #include <sys/time.h>    // for gettimeofday()
    #include <unistd.h>        // for sleep()
    
    int main()
    {
        struct timeval start, end;
        gettimeofday( &start, NULL );
        printf("start : %d.%d
    ", start.tv_sec, start.tv_usec);
        sleep(1);
        gettimeofday( &end, NULL );
        printf("end   : %d.%d
    ", end.tv_sec, end.tv_usec);
    
        return 0;
    }

    3、编译

    g++ -o temp temp.cpp

    4、运行及结果

    $ ./temp 
    start : 1418118324.633128
    end   : 1418118325.634616

    5、usleep函数

    #include <unistd.h>
    usleep(time);// 百万分之一秒

    作者:风波

    mail : fengbohello@qq.com

  • 相关阅读:
    redis
    libevent简述
    IPC-本地套接字
    广播和组播
    UDP实现并发服务器
    select
    epoll
    BUUCTF-[极客大挑战 2019]HardSQL 1
    CTFHUB-Injection V2.0
    SQL注入中登录框只验证用户名情况下的绕过
  • 原文地址:https://www.cnblogs.com/fengbohello/p/4153831.html
Copyright © 2011-2022 走看看