直接上代码吧,没啥好说的。头疼。
#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;
};