C++实现计时类

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;
        }
    }

使用

使用样例如下:

  1. 构造计时类并设定时间单位
  2. 调用start()记录起始点开始计时
  3. 每次调用finish()就会统计距离开始点的时间
  4. 调用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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值