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;
}