基于C++11实现的性能耗时统计组件 - 202204

本文介绍了一个使用C++11编写的独立于平台的性能测试组件,支持端到端和累加两种类型的耗时统计。该组件通过头文件包含即可使用,能够方便地在多线程环境中进行性能测试,并输出详细的报告。测试代码中展示了如何在单线程和多线程场景下使用该组件,并将报告输出到文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

平时经常需要对代码进行性能测试,自己用C++写了一个耗时统计组件,采用C++11标准,不依赖任何特定平台,包含文件即可使用。

// PerfTime.h

#ifndef _CPP_PERFTIME_H_
#define _CPP_PERFTIME_H_

namespace PerfTime {
    // 耗时统计类型
    enum class PerfType {
        PERF_E2E = 0, // 端到端类型,支持多线程
        PERF_ACC      // 累加类型,支持多线程
    };

    void StartHit(PerfType type, const char *mark); // 开始打点
    void EndHit(PerfType type, const char *mark); // 结束打点
    void OutputReport(const char *file, bool isOutputCmd = true); // 输出报告
}

#endif // _CPP_PERFTIME_H_
// PerfTime.cpp

#include "PerfTime.h"
#include <string>
#include <vector>
#include <map>
#include <mutex>
#include <chrono>
#include <algorithm>
#include <numeric>
#include <fstream>
#include <sstream>
#include <iostream>

namespace PerfTime {
    using UINT64 = unsigned long long;
    //using namespace std::chrono;

    // 打点类型
    struct HitInfo {
        PerfType type;
        UINT64 timePoint;
        bool isStart = true;

        HitInfo(UINT64 tp, PerfType type, bool isStart) : timePoint(tp), type(type), isStart(isStart) {}
    };

    // 统计数据
    struct StatisticsInfo {
        UINT64 callCount;
        UINT64 costTime;

        StatisticsInfo(UINT64 call, UINT64 time) : callCount(call), costTime(time) {}
    };

    // 统计功能实现类
    class Interface {
    public:
        static void StartHit(PerfType type, const std::string &am
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值