C++ 时间处理-统计函数运行时间

  • [1. 关键词](#1. 关键词)
  • [2. 问题](#2. 问题)
  • [3. 解决思路](#3. 解决思路)
  • [4. 代码实现](#4. 代码实现)
    • [4.1. timecount.h](#4.1. timecount.h)
    • [4.2. timecount.cpp](#4.2. timecount.cpp)
  • [5. 测试代码](#5. 测试代码)
  • [6. 运行结果](#6. 运行结果)
  • [7. 源码地址](#7. 源码地址)

1. 关键词

C++ 时间处理 统计函数运行时间 跨平台

2. 问题

C++如何简单便捷地实现"函数运行时间的统计"功能?

3. 解决思路

  • 类的构造函数:会在对象初始化的时候被调用。
  • 类的析构函数:会在对象销毁的时候被调用。
  • 局部对象的生命周期:对象实例化(也就是初始化)时开始,退出作用域时结束。

4. 代码实现

4.1. timecount.h

cpp 复制代码
#pragma once

#include <cstdint>
#include <atomic>
#include <string>

namespace cutl
{
    /**
     * @brief A simple time counter class to measure the execution time of a function.
     *
     */
    class timecount
    {
    public:
        /**
         * @brief Construct a new timecount object
         * The constructor will record the begin time of the function calling.
         * @param func_name
         */
        timecount(const std::string &func_name);
        /**
         * @brief Destroy the timecount object
         * The desctructor will record the end time of the function calling and calculate the execution time.
         */
        ~timecount();

    private:
        std::string func_name_;
        std::atomic<uint64_t> start_time_;
    };

} // namespace

4.2. timecount.cpp

cpp 复制代码
#include "timecount.h"
#include "timeutil.h"
#include "strfmt.h"
#include "inner/logger.h"

namespace cutl
{
    timecount::timecount(const std::string &func_name)
        : func_name_(func_name)
    {
        start_time_ = clocktime(timeunit::us);
    }

    timecount::~timecount()
    {
        auto end_time = clocktime(timeunit::us);
        auto duration = end_time - start_time_;
        auto text = "[timecount] " + func_name_ + " used " + fmt_timeduration_us(duration);
        CUTL_LOGGER.info("", text);
    }
} // namespace

5. 测试代码

cpp 复制代码
#pragma once

#include <iostream>
#include "timecount.h"
#include "common.hpp"

void TestTimecount()
{
    PrintTitle("timecount");

    cutl::timecount tcount("TestTimecount");
    std::cout << "TestTimecount begin" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "TestTimecount end" << std::endl;
}

6. 运行结果

bash 复制代码
=============================================timecount==============================================
TestTimecount begin
TestTimecount end
[2024-05-19 22:34:35.853][I]]0x7ff844a9b100](cutl)  [timecount] TestTimecount used 01s.004955us

7. 源码地址

更多详细代码,请查看本人写的C++ 通用工具库: common_util, 本项目已开源,代码简洁,且有详细的文档和Demo。

相关推荐
黑不溜秋的2 小时前
C++ 设计模式 - 策略模式
c++·设计模式·策略模式
Dream it possible!4 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
柠石榴4 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
王老师青少年编程4 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛
澄澈天空6 小时前
C++ MFC添加RichEditControl控件后,程序启动失败
c++·mfc
Lzc7747 小时前
C++初阶——简单实现vector
c++·简单实现vector
一个小白17 小时前
C++——list模拟实现
开发语言·c++
程序员老舅8 小时前
C++ Qt项目教程:WebServer网络测试工具
c++·qt·测试工具·webserver·qt项目·qt项目实战
靡不有初1118 小时前
CCF-CSP第18次认证第一题——报数【两个与string相关的函数的使用】
c++·学习·ccfcsp
cookies_s_s9 小时前
Linux--进程(进程虚拟地址空间、页表、进程控制、实现简易shell)
linux·运维·服务器·数据结构·c++·算法·哈希算法