c++ 计算程序运行时间

方式1

#define VA_ARGS_TO_STRING(...) DrawUnitDebug::vaArgsToString(__VA_ARGS__)
#ifdef DRAW_UNIT_DEBUG
//计时开始
#define TIMING_START(...) static auto time_point_start = std::chrono::steady_clock::now();\
	static auto time_point_end = std::chrono::steady_clock::now(); \
	static std::chrono::duration<double, std::milli>period_spend_time=time_point_end - time_point_start ;\
	time_point_end = std::chrono::steady_clock::now(); \
	period_spend_time = time_point_end - time_point_start;\
	ALOGE("\n start use time outside :%lf : %s",period_spend_time.count(),VA_ARGS_TO_STRING(__VA_ARGS__).c_str());\
	time_point_start = std::chrono::steady_clock::now();

//计时结束
#define TIMING_END(...)  time_point_end = std::chrono::steady_clock::now();\
	period_spend_time = time_point_end - time_point_start;\
	time_point_start = std::chrono::steady_clock::now();\
	ALOGE("\n end time:%lf : %s", period_spend_time.count(),VA_ARGS_TO_STRING(__VA_ARGS__).c_str());\

#else
#define TIMING_START //
#define TIMING_END //
#endif

//
std::string DrawUnitDebug::vaArgsToString(const char *msg, ...){
	char* contentMsg;
	va_list vaList;
	va_start(vaList, msg);
	vasprintf(&contentMsg, msg, vaList);
	va_end(vaList);
	return std::string(contentMsg);
}

方式2

class calcRunTime{
public:
	calcRunTime(std::string t){
		tag=t;
		startTime = clock();
		lastTime = clock();
	}
	void printTime(std::string t,std::string msg){
		tag=t;
		clock_t now_time=clock();
#ifdef ANDROID
		double tim=(double)(now_time - lastTime)/ 1000.0;
		if(tim>20)
			ALOGE(" calcRunTime-- %s [%s]: %lf ms",t.c_str(),msg.c_str(),tim);
#else
		printf(" calcRunTime-- %s [%s]: %lf ms",t.c_str(),msg.c_str(),(double)(now_time - lastTime)/ 1000.0);
#endif
		lastTime = clock();
	}
	void restart(std::string t){
		tag=t;
		startTime=clock();
	}
	void setTag(std::string t){
		tag=t;
	}
	~calcRunTime(){
		lastTime=startTime;
		printTime(tag,"end of timing, total time");
	}
	void calcTime(){//test
		std::chrono::steady_clock::time_point start= std::chrono::steady_clock::now();
		std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
		float m_drawFrameTime = std::chrono::duration_cast<std::chrono::microseconds>(now - start).count() / 1000.0f;
	}
	static calcRunTime* static_ctr_p;
private:
	std::string tag="";
	clock_t startTime,endTime,lastTime;

};

//方式一: 需要在同一个语句块成对出现,多线程(推荐)
#define CALC_TIME_START(index) {calcRunTime crt##index(__FUNCTION__);
#define CALC_TIME_SPAN(index,msg) crt##index.printTime(__FUNCTION__,msg);
#define CALC_TIME_RESET(index) crt##index.restart(__FUNCTION__);
#define CALC_TIME_END(index) crt##index.setTag(__FUNCTION__);}
//方式2: 跨程序块执行,跨线程全局变量需要做维护,多线程算了
#define CALC_TIME_START1(crt_ptr) crt_ptr=new calcRunTime(__FUNCTION__);
#define CALC_TIME_SPAN1(msg,crt_ptr) crt_ptr->printTime(__FUNCTION__,msg);
#define CALC_TIME_RESET1 crt_ptr->restart(__FUNCTION__);
#define CALC_TIME_END1(crt_ptr) crt_ptr->setTag(__FUNCTION__);delete crt_ptr;
//方式3: 任意地方使用,多线程算了
#define CALC_TIME_START2 if(calcRunTime::static_ctr_p== nullptr)calcRunTime::static_ctr_p=new calcRunTime(__FUNCTION__);
#define CALC_TIME_SPAN2(msg) calcRunTime::static_ctr_p->printTime(__FUNCTION__,msg);
#define CALC_TIME_RESET2 calcRunTime::static_ctr_p->restart(__FUNCTION__);
#define CALC_TIME_END2 calcRunTime::static_ctr_p->setTag(__FUNCTION__);delete calcRunTime::static_ctr_p; calcRunTime::static_ctr_p=nullptr;

使用:

//方式1
{
	CALC_TIME_START(0);
	...
	CALC_TIME_SPAN(0,"test1")
	...
	CALC_TIME_END(0) 
}
//方式2
	calcRunTime* ctr_p;
	CALC_TIME_START1(ctr_p);
	{
		...
		CALC_TIME_SPAN1("test2",ctr_p)
		...
		CALC_TIME_END1(ctr_p)
	}
//方式3
calcRunTime* calcRunTime::static_ctr_p= nullptr;
//任意位置
CALC_TIME_START2
CALC_TIME_SPAN2("test3")
CALC_TIME_END2

结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值