zoukankan      html  css  js  c++  java
  • 计算程序运行时间的封装

    只能在linux下使用:

    #include <sys/time.h>
    
    class timer
    {
        public:
            timer(int _timerType = CLOCK_PROCESS_CPUTIME_ID) : timerType(_timerType)
            {
                clock_gettime(timerType, &begin);
            }
            long elapsed() const
            {
                timespec end;
                clock_gettime(timerType, &end);
                timespec temp;
                if ((end.tv_nsec-begin.tv_nsec)<0) {
                    temp.tv_sec = end.tv_sec-begin.tv_sec-1;
                    temp.tv_nsec = 1000000000+end.tv_nsec-begin.tv_nsec;
                } else {
                    temp.tv_sec = end.tv_sec-begin.tv_sec;
                    temp.tv_nsec = end.tv_nsec-begin.tv_nsec;
                }
                return temp.tv_sec*1000*1000*1000 + temp.tv_nsec;
            }
            void restart()
            {
                 clock_gettime(timerType, &begin);
            }
        private:
            timespec begin;
            int timerType;
    };

    链接时需要 -lrt

    使用如下:

    timer t;
    int *pi = new int;
    std::cout<<t.elapsed()<<std::endl;
    
    t.restart();
    char *pc = new char[3];
    std::cout<<t.elapsed()<<std::endl;

    测试函数输出时间:

    #include <sys/time.h>
    #include <string>
    #include <iostream>
    
    class scoped_timer
    {
        public:
            scoped_timer(std::string _funName = "", int _timerType = CLOCK_PROCESS_CPUTIME_ID) : funName(_funName),timerType(_timerType)
            {
                clock_gettime(timerType, &begin);
            }
            ~scoped_timer()
            {
                std::cout<<funName<<" : "<<elapsed()<<"(ms)"<<std::endl;  //这里也可以输出到文件
            }
            private:
                long elapsed() const
                {
                    timespec end;
                    clock_gettime(timerType, &end);
                    timespec temp;
                    if ((end.tv_nsec-begin.tv_nsec)<0) {
                        temp.tv_sec = end.tv_sec-begin.tv_sec-1;
                        temp.tv_nsec = 1000000000+end.tv_nsec-begin.tv_nsec;
                    } else {
                        temp.tv_sec = end.tv_sec-begin.tv_sec;
                        temp.tv_nsec = end.tv_nsec-begin.tv_nsec;
                    }
                    return temp.tv_sec*1000*1000*1000 + temp.tv_nsec;
                }
        private:
            timespec begin;
            int timerType;
            std::string funName;
    };
    #ifdef DEBUG
    #define funTime(funname) scoped_timer t(funname);
    #else
    #define funTime(funname)
    #endif

    测试程序如下:

    void fun()
    {
        funTime("fun()");
        for(int i = 0; i < 1000000; i++)
            int *pi = new int;
    }

    编译时使用:g++ test6.cpp -o test6 -lrt -DDEBUG  执行后就会输出函数消耗时间

  • 相关阅读:
    2单表CRUD综合样例开发教程
    1代码表样例开发教程
    ProcessOn
    UniEAP Platform V5.0 Unable to compile class for JSP
    Message Unable to compile class for JSP
    UniEAP Platform V5.0建库
    UML_2_浅谈UML的概念和模型之UML九种图
    PPT鼠绘必须掌握的PPT绘图三大核心功能
    PPT添加节
    remap.config文件配置模板
  • 原文地址:https://www.cnblogs.com/tianyajuanke/p/3300944.html
Copyright © 2011-2022 走看看