zoukankan      html  css  js  c++  java
  • 获取程序段执行时间

    struct timeval pre_time, now_time;
    unsigned int time;

    gettimeofday(&pre_time, NULL);
    要测试的程序段
    gettimeofday(&now_time, NULL);


    time = (now_time.tv_sec - pre_time.tv_sec) * 1000000;
    time += now_time.tv_usec;
    time -= pre_time.tv_usec;

    printf("pass time = %d [us] ", time);

    这是错误的
    /*
    * start_stop : 0 start,1 stop * distinguish: used to distinguish each other */ void print_time(int start_stop, int distinguish) { uint64_t diff_time, diff_sec, diff_usec; static struct timeval pre_time, now_time; if(!start_stop) { pre_time.tv_sec = 0; pre_time.tv_usec = 0; now_time.tv_sec = 0; now_time.tv_usec = 0; gettimeofday(&pre_time, NULL); } else { gettimeofday(&now_time, NULL); diff_sec = now_time.tv_sec - pre_time.tv_sec; diff_usec = now_time.tv_usec - pre_time.tv_usec; diff_time = (diff_sec * 1000000 + diff_usec) / 1000; dbg_printf("[%d]: diff_time=%lu ms ", distinguish, diff_time); } }
    这是错误的!
    /*
    * start_stop : 0 start,1 stop * distinguish: used to distinguish each other */ void print_time(int start_stop, int distinguish) { uint32_t diff_time, diff_sec, diff_usec; static struct timeval pre_time, now_time; if(!start_stop) { pre_time.tv_sec = 0; pre_time.tv_usec = 0; now_time.tv_sec = 0; now_time.tv_usec = 0; gettimeofday(&pre_time, NULL); } else { gettimeofday(&now_time, NULL); diff_sec = now_time.tv_sec - pre_time.tv_sec; diff_usec = now_time.tv_usec - pre_time.tv_usec; diff_time = diff_sec * 1000 + diff_usec / 1000; dbg_printf("[%d]: diff_time=%d ms ", distinguish, diff_time); } }
    /*
     * start_stop : 0 start,1 stop
     * distinguish: used to distinguish each other
     */
    void print_time(int start_stop, int distinguish)
    {
        uint64_t diff_time, diff_sec, diff_usec;
        static struct timeval pre_time, now_time;
    
        if(!start_stop) {
            pre_time.tv_sec  = 0;
            pre_time.tv_usec = 0;
            now_time.tv_sec  = 0;
            now_time.tv_usec = 0;
            gettimeofday(&pre_time, NULL);
        } else {
            gettimeofday(&now_time, NULL);
        
            diff_sec  = now_time.tv_sec  - pre_time.tv_sec;
            diff_usec = now_time.tv_usec > pre_time.tv_usec ?
                now_time.tv_usec - pre_time.tv_usec :
                pre_time.tv_usec - now_time.tv_usec ;
            diff_time = (diff_sec * 1000000 + diff_usec) / 1000;
            dbg_printf("[%d]: diff_time=%lu ms
    ", distinguish, diff_time);
        }
    }

    这个比较好用:

    #define print_log_with_time(fmt, args...)                                          
    do {                                                                          
            struct timespec ts;                                               
            clock_gettime(CLOCK_MONOTONIC,&ts);                               
            fprintf(stderr,"[%5lu.%06lu] initsvscripts %s:%d: " fmt "
    ",    
                        ts.tv_sec, ts.tv_nsec / 1000, __func__, __LINE__, ##args);    
        } while (0)
  • 相关阅读:
    PHP vscode+XDebug 远程断点调试服务器上的代码
    Wordpress 为用户或角色 role 添加 capabilities(权限)
    Wordpress 后台文章编辑区添加模板选择功能
    CentOS 7 编译安装最新版git
    WordPress 通过文章 URL 获取文章 ID
    Web 安全问题 rel="noopener nofollw"
    Wordpress 通过 post id 获取文章 url
    git放弃修改&放弃增加文件
    Wordpress 作者模板页中的自定义帖子类型分页问题
    PHP 删除 url 中的 query string
  • 原文地址:https://www.cnblogs.com/hellokitty2/p/8053839.html
Copyright © 2011-2022 走看看