惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
博客园 - 【当耐特】
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
MyScale Blog
MyScale Blog
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Hugging Face - Blog
Hugging Face - Blog
I
InfoQ
B
Blog RSS Feed
腾讯CDC
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
雷峰网
雷峰网
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
DataBreaches.Net
The Cloudflare Blog
V
V2EX
S
SegmentFault 最新的问题

又见苍岚

COLMAP PatchMatch Stereo 算法详解 事件驱动的状态机框架:从理论到工程实践 Git 在国内网络环境下无法 Push 的排查与修复 —— 配置 Clash 代理 分段五次多项式插值原理详解 路径插值方法深度对比研究 Claude Code 使用指南 OpenClaw 记忆管理与技能创建指南 CBS(Conflict-Based Search)算法详解 A* 算法及其变种详解 OpenClaw 配置多 Agents Windows Powershell 无法加载文件,因为在此系统上禁止运行脚本问题的解决方案 MaxClaw 安装流程 大模型 AI 名词介绍 AList 网盘聚合工具简介 Protobuf 简介与测试 Claude Code 简介以及 GLM 4.7 模型接入 Github 歌词下载工具 163MusicLyrics Python __getattr__ 懒加载 Python TypedDict 机器人仿真平台 Gazebo 安装记录 机器人仿真平台 Gazebo 简介 多机器人路径规划问题(Multi-Agent Path Finding, MAPF)简介 Python exifread 读取修改过的 jpeg 信息错误问题修复 3D 坐标系变换的理解 3D 旋转矩阵基本概念 MongoDB Compass 介绍 Python 环境管理工具 uv Flutter 开发指南 Snipaste 安装下载与黑屏问题解决方案 全局路径规划算法记录
C++ 计时
Yiwei Zhang · 2023-03-22 · via 又见苍岚

C++ 中计时是度量系统性能的常用方法,本文记录 C++ 常用计时方法。

time.h

time.h 是最常用的 C++ 计时头文件,在 C++ 中,计时通常使用 <time.h> 头文件中的 clock() 函数记录CPU 单元的运行周期时间,可以在 Windows / Linux 等操作系统中使用,配合 CLOCKS_PER_SEC 实现对真实事件单位秒(s)等的转换。

  • 两次调用 clock() 函数,差值表示程序运行开始和结束时刻之间的CPU时钟计时单元(clock tick)数;
  • CLOCKS_PER_SEC,表示一秒钟会有多少个时钟计时单元(clock tick);
  • 设差值为 durationduration / CLOCKS_PER_SEC 就为秒。

数据类型

计时得到的常用数据类型是 clock_ttime_t, 在 time.h 文件中,我们可以找到对它们的定义:

  • clock_t
1
2
3
4
5
6
7
#ifndef _CLOCK_T_DEFINED 

  typedef long clock_t;

  #define _CLOCK_T_DEFINED

  #endif

说明 clock_t 类型本质就是 long 类型

  • time_t
1
2
3
4
5
6
7
8
9
10
typedef long                          __time32_t;
typedef __int64 __time64_t;

#ifndef _CRT_NO_TIME_T
#ifdef _USE_32BIT_TIME_T
typedef __time32_t time_t;
#else
typedef __time64_t time_t;
#endif
#endif

说明 time_t 可能是 32 位整形或 64 位整形

  • 总之时间本质就是一个大整形数据

计时间隔

一个 clock 表示一个计时间隔,每经过一个计时间隔的时间这个计时的整数会增加 1

计时间隔 和真实时间的联系靠的是 CLOCKS_PER_SEC,这个宏的含义是一秒钟有多少个计时间隔

在定义中 CLOCKS_PER_SEC 是 1000

1
#define CLOCKS_PER_SEC  ((clock_t)1000)

也就表示一个 计时间隔 表示 1ms 的时间

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>

using namespace std;

int main(void)
{
long i = 100000000L;
time_t start, finish;
double duration;
/* 测量一个事件持续的时间*/
printf("Time to do %ld empty loops is ", i);
start = clock();
while (i--)
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("%f seconds\n", duration);
return 0;
}

输出

1
Time to do 100000000 empty loops is 0.148000 seconds

参考资料

文章链接:
https://www.zywvvd.com/notes/coding/cpp/cpp-timer/cpp-timer/