C++实现一个计时类
使用C++想要方便快捷高精度的统计程序运行时间?
使用标准库chrono头进行封装,实现统计计时开始点与多个结束点之间的时间.
#include <chrono>
全部代码在最后
准备
首先我们需要包含chrono以计时,以及iostream用来输出信息.
chrono的一些函数使用std::chrono
命名空间,所以using namespace std::chrono;
我们的代码为了不与其他命名冲突定义timer
命名空间
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;
namespace timer {
......
}
计时类
首先需要定义我们选择显示的时间单位,构造函数以及存储显示单位的变量
定义单位使用枚举变量
构造函数默认使用毫秒单位
class amazcuter_timer {
public:
// 可选择的时间单位:秒,毫秒,微秒
enum unit { s, milli_s, micro_s };
// 构造函数,默认时间单位毫秒
amazcuter_timer (unit in_unit = milli_s) : timeunit(in_unit) {}
...计时函数...
private:
...时间节点...
unit timeunit;
}
时间节点
使用chrono的time_point模板类,模版类型用chrono的high_resolution_clock(要统计程序运行时间嘛,精度高点)
定义一个开始,一个结束
private:
time_point<high_resolution_clock> begin;
time_point<high_resolution_clock> end;
计时函数
开始计时将begin
设为当前系统时钟,结束计时将end
设为当前系统时钟并输出.
这样多次调用finish()
就可以多点计时了
// 输出当前系统时间
void now_time() {
auto now = system_clock::now();
time_t now_c = system_clock::to_time_t(now);
cout << "当前时间为:" << ctime(&now_c) << endl;
}
// 开始计时
void start() { begin = high_resolution_clock::now(); }
// 根据选择的时间单位,与计时开始的时间,cout时间间隔
void finish(string info=string()) {
end = high_resolution_clock::now();
cout << endl << info << "-花费时间:";
switch (timeunit) {
case s:
cout << duration_cast<seconds>(end - begin).count() << " s" << endl;
break;
case milli_s:
cout << duration_cast<milliseconds>(end - begin).count() << " ms"
<< endl;
break;
case micro_s:
cout << duration_cast<microseconds>(end - begin).count() << " μs"
<< endl;
break;
}
}
使用
使用样例如下:
- 构造计时类并设定时间单位
- 调用
start()
记录起始点开始计时 - 每次调用
finish()
就会统计距离开始点的时间 - 调用
start()
重新开始计时
#include "amazcuter_time.h"
#include <iostream>
using namespace std;
using namespace timer;
int main(void) {
amazcuter_timer timer(amazcuter_timer::milli_s);
timer.now_time();
timer.start();
system("pause");
timer.finish();
system("pause");
timer.finish();
return 0;
}
所有代码
amazcuter_time.h:
/**
* @file amazcuter_time.h
* @author amazcuter (amazcuter@outlook.com)
* @brief This header file is a module used to measure the time between two
* points of program execution. It contains a amazcuter_timer class, which has a
* constructor. The default time unit is milliseconds. There are three
* functional functions that output the current system time, start timing, end
* timing and output information. You can set the time unit in the constructor
* parameter.
* @version 0.1
* @date 2023-06-14
*
* @copyright Copyright (c) 2023
*
*/
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;
namespace timer {
class amazcuter_timer {
public:
// 可选择的时间单位:秒,毫秒,微秒
enum unit { s, milli_s, micro_s };
// 构造函数,默认时间单位毫秒
amazcuter_timer(unit in_unit = milli_s) : timeunit(in_unit) {}
// 输出当前系统时间
void now_time() {
auto now = system_clock::now();
time_t now_c = system_clock::to_time_t(now);
cout << "当前时间为:" << ctime(&now_c) << endl;
}
// 开始计时
void start() { begin = high_resolution_clock::now(); }
// 根据选择的时间单位,与计时开始的时间,cout时间间隔
void finish(string info=string()) {
end = high_resolution_clock::now();
cout << endl << info << "-花费时间:";
switch (timeunit) {
case s:
cout << duration_cast<seconds>(end - begin).count() << " s" << endl;
break;
case milli_s:
cout << duration_cast<milliseconds>(end - begin).count() << " ms"
<< endl;
break;
case micro_s:
cout << duration_cast<microseconds>(end - begin).count() << " μs"
<< endl;
break;
}
}
private:
time_point<high_resolution_clock> begin;
time_point<high_resolution_clock> end;
unit timeunit;
};
} // namespace timer
main.cpp
#include "amazcuter_time.h"
#include <iostream>
using namespace std;
using namespace timer;
int main(void) {
amazcuter_timer timer(amazcuter_timer::milli_s);
timer.now_time();
timer.start();
system("pause");
timer.finish();
system("pause");
timer.finish();
return 0;
}