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);
  • 相关阅读:
    广度优先搜索
    洛谷 P1126 机器人搬重物
    codevs 1058 合唱队形
    洛谷P1216 [USACO1.5]数字三角形 Number Triangles
    Codevs 1576 最长严格上升子序列
    跳马(Knight Moves), ZOJ1091, POJ2243
    洛谷 P1644 跳马问题
    NOI 2971 抓住那头牛
    NOI 2727 仙岛求药
    搜索与回溯算法
  • 原文地址:https://www.cnblogs.com/csun/p/10582770.html
Copyright © 2011-2022 走看看