程序运行时间计算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);
原文地址:https://www.cnblogs.com/csun/p/10582770.html