linux cpp中精确获取程序的执行时间

clock_gettime使用示例:

struct timespec start;
struct timespec end;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&start);
long i =0;
while(i<10e8)
{
    i++;
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&end);
double start_ms =start.tv_sec*1000+start.tv_nsec/1000000;
double end_ms =end.tv_sec*1000+end.tv_nsec/1000000;
printf("clock_gettime执行时间:%.4lf ms\n",end_ms - start_ms);

//用法示例

timeWatch timeWatcher;//1 构造对象
timeWatcher.start();//2 调用start函数

function(); //此处为要监控的函数或者语句

double ns = timeWatcher.get_ns();
double us = timeWatcher.get_us();
double ms = timeWatcher.get_ms();//3 获取时间

cout<<"程序执行耗时"<< ns <<"ns"<<endl;
cout<<"程序执行耗时"<< us <<"us"<<endl;
cout<<"程序执行耗时"<< ms <<"ms"<<endl;//4 输出到控制台

完整程序:

#include <iostream>
#include <time.h>
using namespace std;

class timeWatch
{

public:
    void start() noexcept{
        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &this->start_ts);
        this->is_started=true;
    }

    long get_ns() const noexcept{//纳秒
        struct timespec end_ts;
        clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&end_ts);
        long dur_s=end_ts.tv_sec-start_ts.tv_sec;
        long dur_ns=dur_s * 1000000000L + end_ts.tv_nsec - start_ts.tv_nsec;
        return dur_ns;
    }

    double get_us() const noexcept{  //微秒
            
        return get_ns()/1000.0;
    }
    double get_ms() const noexcept{  //毫秒
            
        return get_ns()/1000000.0;
    }

private:
    struct timespec start_ts;
    bool is_started;

};

int main()
{

timeWatch timeWatcher;
timeWatcher.start();

long i =0;
while(i<10e8)
{
    i++;
}

double ns = timeWatcher.get_ns();
double us = timeWatcher.get_us();
double ms = timeWatcher.get_ms();

cout<<"程序执行耗时"<< ns <<"ns"<<endl;
cout<<"程序执行耗时"<< us <<"us"<<endl;
cout<<"程序执行耗时"<< ms <<"ms"<<endl;

return 0;

}

重写可输出函数名:

#include <iostream>
#include <time.h>
#include <string>
using namespace std;

class timeWatch
{

public:
    void start(string str) noexcept{
        cout<<"begin to watch:"<< str << endl;
        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &this->start_ts);
        this->is_started=true;
    }

    long get_ns() const noexcept{//纳秒
        struct timespec end_ts;
        clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&end_ts);
        long dur_s=end_ts.tv_sec-start_ts.tv_sec;
        long dur_ns=dur_s * 1000000000L + end_ts.tv_nsec - start_ts.tv_nsec;
        return dur_ns;
    }

    double get_us() const noexcept{  //微秒
            
        return get_ns()/1000.0;
    }
    double get_ms() const noexcept{  //毫秒
            
        return get_ns()/1000000.0;
    }
    void get_str_ms(string str) const noexcept{  //毫秒
            
        cout<<"end "<< str << " time usage:" << get_ns()/1000000.0 << "ms" <<endl;
        cout<<"-------------------------------"<<endl;
    }
private:
    struct timespec start_ts;
    bool is_started;

};

int main()
{

timeWatch timeWatcher;
timeWatcher.start("test");
long i =0;
while(i<10e8)
{
    i++;
}


double ns = timeWatcher.get_ns();
double us = timeWatcher.get_us();
double ms = timeWatcher.get_ms();
timeWatcher.get_str_ms("test");

cout<<"程序执行耗时"<< ns <<"ns"<<endl;
cout<<"程序执行耗时"<< us <<"us"<<endl;
cout<<"程序执行耗时"<< ms <<"ms"<<endl;

//cout<<"-------------------------------"<<endl;
timeWatcher.start("test");//传入函数名
long j =0;
while(j < 10e8)
{
    j++;
}
timeWatcher.get_str_ms("test");//结束调用并自动打印耗时

//cout<<"-------------------------------"<<endl;
timeWatcher.start("test2");
long k =0;
while(k < 10e8)
{
    k++;
}
timeWatcher.get_str_ms("test2");

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值