c++性能分析工具类 统计程序每一步的执行时间

ops/2024/9/22 19:45:11/

c++性能分析工具类 统计程序每一步的执行时间

在c++中,经常需要做性能分析,统计 不同的函数,不同的代码块的执行时间,为了方便统计,实现了一个 统计类
有两个函数,第一个是统计从 时间统计类 从初始化到当前调用的次数,第二个是 记录运行了多久,第三个是统计 本次调用和上一次调用的时间差距,并打印当前的时间的年月日时分秒,还自定义地打印一下本次的标记信息。

效果如下
在这里插入图片描述

类如下

#include <iostream>
#include <chrono>
#include <vector>
#include <string>
#include <ctime>
#include <iostream>
#include <cmath> #include <iostream>
#include <sstream>
#include <iomanip>#ifdef _WIN32
#include <Windows.h>
#endifclass TimeLogger {
public:TimeLogger() {start_time = std::chrono::system_clock::now();previous_time = start_time;time_number = 0;}void get_now_time(std::string show_str = "") {auto current_time = std::chrono::system_clock::now();auto time_diff = std::chrono::duration<double>(current_time - previous_time).count();previous_time = current_time;std::time_t current_time_t = std::chrono::system_clock::to_time_t(current_time);std::string current_time_str = std::ctime(&current_time_t);SetConsoleOutputCP(65001); // 设置控制台输出编码为 UTF-8// Remove newline characterif (!current_time_str.empty() && current_time_str[current_time_str.size() - 1] == '\n') {current_time_str[current_time_str.size() - 1] = '\b'; // Replace '\n' with '\b'}if (show_str != "") {// std::cout << show_str;std::cout << "["  << time_number<< "\t  time--->" <<this->format_float_to_3decimals(this->get_time_since_init())<<"s \t" << " " << time_diff << "s \t" << current_time_str <<" \t"<<show_str<< "]" << std::endl;}else{std::cout << "["  << time_number<< "\t  time--->" <<this->format_float_to_3decimals(this->get_time_since_init())<<"s \t" << " " << time_diff << "s \t" << current_time_str << "]" << std::endl;}time_number++;}double get_time_since_init() {auto current_time = std::chrono::system_clock::now();auto time_diff = std::chrono::duration<double>(current_time - start_time).count();// Get current time in time_t format and convert it to a stringstd::time_t current_time_t = std::chrono::system_clock::to_time_t(current_time);std::string current_time_str = std::ctime(&current_time_t);// Remove newline character from current_time_strcurrent_time_str = current_time_str.substr(0, current_time_str.size() - 1);// Return time difference with 3 decimal pointsreturn std::round(time_diff * 1000) / 1000.0;}std::string format_float_to_3decimals(double input_float) {std::stringstream ss;ss << std::fixed << std::setprecision(3) << input_float;std::string formatted_float = ss.str();// Pad with zeros if necessarysize_t decimal_pos = formatted_float.find(".");if (decimal_pos != std::string::npos && formatted_float.size() - decimal_pos < 4) {formatted_float += "0";}return formatted_float;
}private:std::chrono::time_point<std::chrono::system_clock> start_time;std::chrono::time_point<std::chrono::system_clock> previous_time;int time_number;
};int main() {TimeLogger logger;logger.get_now_time("初始化");for(int i=0;i<=100;i++){logger.get_now_time("中文修改中 "+std::to_string(i));Sleep(100);}logger.get_time_since_init();return 0;
}

http://www.ppmy.cn/ops/34010.html

相关文章

IFPP-01

这份文件是一份关于In-Factory Profile Provisioning (IFPP)的立场文件&#xff0c;由物联网转型主题赞助。以下是其核心内容的概述&#xff1a; 1. **IFPP的概念**&#xff1a;IFPP是一种新兴技术&#xff0c;用于简化将蜂窝SIM配置文件嵌入到物联网(IoT)设备和其他连接设备中…

【银角大王——Django课程——靓号搜索实现/单独一篇】

搜索框功能实现 靓号搜索在Django框架中搜索功能实现——底层就是模糊查询添加一个搜索框&#xff0c;使用bootstrap框架将GO&#xff01;修改成一个放大镜靓号列表函数代码解释效果演示 靓号搜索 在Django框架中搜索功能实现——底层就是模糊查询 数字条件用法字符串条件用法…

Spring GA、PRE、SNAPSHOT 版本含义及区别

GA:General Availability: 正式发布的版本&#xff0c;推荐使用&#xff08;主要是稳定&#xff09;&#xff0c;与maven的releases类似&#xff1b; PRE: 预览版,内部测试版。主要是给开发人员和测试人员测试和找BUG用的&#xff0c;不建议使用&#xff1b; SNAPSHOT: 快照…

华为机考入门python3--(22)牛客22- 汽水瓶

分类&#xff1a;数字 知识点&#xff1a; 整除符号// 5//3 1 取余符号% 5%3 2 题目来自【牛客】 import sysdef calc_soda_bottles(n):if n 0: # 结束输入&#xff0c;不进行处理returnelse:# 循环进行汽水换算total_drunk 0 # 记录总共喝了多少瓶汽水while…

ES集群数据备份与迁移

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、文章涉及概念讲解二、操作步骤1.创建 snapshot repository操作主机hadoop1分别操作从机hadoop2和hadoop3 2. 查看仓库信息3. 备份索引&#xff0c;生成快照…

Objective-C高级特性浅析与实践指南

OC的学习笔记&#xff08;二&#xff09; 文章目录 OC的学习笔记&#xff08;二&#xff09;property访问控制符点语法 自定义init方法内存管理retain 和 release class处理发生异常的方法NSSrting的常用方法类方法对象方法lengthcharacterAtIndexisEuqalStringcompare autorel…

QT+串口调试助手+扩展版

前言&#xff1a;此文章是这篇文章的拓展 QT串口调试助手基本版-CSDN博客&#xff0c;如果需要独立完成串口调试助手直接看基本版文章即可&#xff0c;如果需要完成串口调试助手的其他功能&#xff0c;参考拓展版。 一、更新QT串口调试助手UI界面 1、ui串口设置界面 2、ui串口…

Python基础详解一

一&#xff0c;print打印 print("hello word") print(hello word) 双引号和单引号都可以 二&#xff0c;数据类型 Python中常用的有6种值的类型 输出类型信息 print(type(11)) print(type("22")) print(type(22.2)) <class int> <class str&…