zoukankan      html  css  js  c++  java
  • 程序运行时间计算gettimeofday&clock_gettime

    1、clock_t clock(void);   #include <time.h>

      The clock() function returns an approximation of processor time used by the program.

    2、 gettimeofday  #include <sys/time.h>

      gives the number of seconds and microseconds since the Epoch()

        struct timeval tv, tv1;
        gettimeofday(&tv, NULL);
    //-------do something
        gettimeofday(&tv1, NULL);
        int speedtime = (tv1.tv_sec * 1000000 + tv1.tv_usec) - (tv.tv_sec * 1000000 +     tv.tv_usec);
    

     3、 clock_gettime  #include <time.h>

      Its time represents seconds and nanoseconds since the Epoch

      它的时间代表自时代以来的秒和纳秒。当时间发生变化时,相对间隔的计时器不受影响,但绝对时间点的计时器会受到影响。可以实现更多的时钟。对相应时间值的解释和对计时器的影响未指定。  

      typedef unsigned long int uint64_t;   
        uint64_t us = 0;    
        struct timespec tp, tp1;
        clock_gettime(CLOCK_REALTIME, &tp);  //CLOCK_REALTIME  System-wide real-time clock(全系统实时时钟).  Setting this clock requires appropriate privileges
    //----do something
        clock_gettime(CLOCK_REALTIME, &tp1);
        us = (tp1.tv_sec * 1000000000 + tp1.tv_nsec) - (tp.tv_sec * 1000000000 + tp.tv_nsec);
  • 相关阅读:
    TCP定时器 之 重传/延迟ACK/保活 定时器初始化
    指针03-指针和字符串
    指针02
    指针01
    switch语句分析
    结构体分析
    参数、返回值、局部变量、数组分析
    多维数组分析
    循环语句分析
    if语句分析
  • 原文地址:https://www.cnblogs.com/csun/p/10582770.html
Copyright © 2011-2022 走看看