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



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值