【C++】【STL】利用chromo来测量程序运行时间与日志时间打印精确到微秒

直接上代码吧,没啥好说的。头疼。

#include <iostream>
#include <string>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <thread>
#include <chrono>

using namespace std;

class LogTime {
public:
	LogTime() {}
	~LogTime() {}
	inline void Start() { this->start = std::chrono::system_clock::now(); }
	inline void End() { this->end = std::chrono::system_clock::now(); }

	std::string GetRunTime() {
		auto us_duration = std::chrono::duration_cast<std::chrono::microseconds>(this->end - this->start);
		auto ms_duration = std::chrono::duration_cast<std::chrono::milliseconds>(this->end - this->start);
		unsigned long ms = ms_duration.count();
		unsigned long us = us_duration.count();
		std::stringstream ss;
		ss << ms << " ms," << us << " us .";
		return ss.str();
	}

	std::string Now() {
		auto now = std::chrono::system_clock::now();
		auto m = now.time_since_epoch();
		auto diff = std::chrono::duration_cast<std::chrono::microseconds>(m).count(); //当前时间显示到微秒
		auto const ms = diff % 1000000;
		//auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(m).count(); //当前时间显示到毫秒
		//auto const ms = diff % 1000;

		std::stringstream ss;
		std::time_t t = std::chrono::system_clock::to_time_t(now);
		ss << std::put_time(std::localtime(&t), "%Y-%m-%d %H.%M.%S") << "." << ms;
		return ss.str();
	}
private:
	std::chrono::system_clock::time_point start;
	std::chrono::system_clock::time_point end;
};

 

转载于:https://my.oschina.net/u/3800567/blog/1834817

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值