最近写毕业论文的程序,发现了一种更加优雅的统计时间方法,记录一下
主要利用<chrono>
这个时间库来实现,里面包含三种时钟:
system_clock
: 起点是1970-01-01 00:00:00 UTCsteady_clock
: 起点是系统启动时间,一般用于得到时间间隔high_resolution_clock
:高分辨率的版本本次使用system_clock
RD.h
#include<iostream> #include <chrono> #define TIMEDEBUG #ifdef TIMEDEBUG #define RD(X) {RunDuration o;o.begin();##X;o.end(#X);} class RunDuration { public: inline void begin() { _start = std::chrono::system_clock::now(); } inline void end(const char * msg) { _end = std::chrono::system_clock::now(); _duration = (_end - _start); std::cout << msg << " execute time :" << double(_duration.count()) << "s\n----------" << std::endl; } private: std::chrono::system_clock::time_point _start; std::chrono::system_clock::time_point _end; std::chrono::duration<double> _duration; }; #else #define RD(X) X; #endif
#include "DDC.h" #include "RD.h" #include<iostream> void test_DDC(); int main() { std::cout << "ddc start from here!" << std::endl; RD(test_DDC()); system("pause"); return 0; }
https://www.cnblogs.com/liuguoyao514257665/p/13323392.html
https://www.cnblogs.com/zhongpan/p/7490657.html