c++时间度量辅助类
#ifndef __TIME_MEASURE_H
#define __TIME_MEASURE_H
#include <sys/time.h>
class time_measure{
public:
/**
* Get the current time
*/
time_measure();
/**
* Calculate the time range(microseconds,i.e.10e-6 second)
* and reset the tv_ to current time
*/
long int time_range();
private:
timeval tv_;
};
#endif
#include "time_measure.h"
time_measure::time_measure(){
gettimeofday(&tv_,0);
}
long int time_measure::time_range(){
timeval tv;
gettimeofday(&tv,0);
long int diff = (tv.tv_sec-tv_.tv_sec)*1000000+(tv.tv_usec-tv_.tv_usec);
gettimeofday(&tv_,0);
return diff;
}
调用代码很简单:
class test{
public:
void print(){
cout<<"print OK"<<endl;
}
};
int main(){
time_measure t;
my_application& my_app = my_application_singleton_holder::Instance();
my_app.insert_property("test",shared_ptr<test>(new test));
any a = my_app.get_property("test");
any_cast<shared_ptr<test> >(a)->print();
cout<<"time is:"<<t.time_range()<<" microseconds"<<endl;
}
摘自:sheismylife专栏
补充:软件开发 , C++ ,