C++ 计时器

文章目录

  • 一、简介
  • 二、实现代码
    • 2.1 windows平台
    • 2.2 C++标准库
  • 三、实现效果

一、简介

有时候总是会用到一些计时的操作,这里也整理了一些代码,包括C++标准库以及window自带的时间计算函数。

二、实现代码

2.1 windows平台

StopWatch.h

#ifndef STOP_WATCH_H
#define STOP_WATCH_H#ifdef _WIN32
#	include <windows.h>
#else 
#	include <sys/time.h>
#endif/// <summary>
/// 基于Windows平台提供的函数,进行时间计算
/// </summary>class StopWatch
{
public:StopWatch(); // 计时器将自动开始~StopWatch();void  start();// 返回启动以来用户经过的时间(以秒为单位)。double elapsed() const;private:#ifdef WIN32LONGLONG  freq_;LONGLONG  start_count_;
#elsetimeval start_time_;
#endif};#endif

StopWatch.cpp

#include "StopWatch.h"
#include <iostream>StopWatch::StopWatch() {start();		//开始计时
}StopWatch::~StopWatch() {}void StopWatch::start() {
#ifdef WIN32LARGE_INTEGER  largeInteger;// 获取高精度性能计数器的频率,这个频率通常以每秒的计数数来衡量。QueryPerformanceFrequency(&largeInteger);freq_ = largeInteger.QuadPart;//获取当前性能计数器的值,它表示自系统启动以来的计数次数。QueryPerformanceCounter(&largeInteger);start_count_ = largeInteger.QuadPart;
#elsegettimeofday(&start_time_, 0);
#endif
}template <class T>
inline T clip_precision(T v, int p) {float tmp = std::pow(10.0f, p);return (static_cast<int>(v * tmp) / tmp);
}double StopWatch::elapsed() const {
#ifdef WIN32LARGE_INTEGER  largeInteger;QueryPerformanceCounter(&largeInteger);LONGLONG now_count = largeInteger.QuadPart;//计算已经经过的时间。//计算当前计数值与起始计数值之间的差值,然后将其除以频率来得到时间,以秒为单位。double time = (double)((now_count - start_count_) / static_cast<double>(freq_));return clip_precision(time, 6);		//保留小数的有效位数
#elsetimeval now;gettimeofday(&now, 0);double time = (now.tv_sec - start_time_.tv_sec) + (now.tv_usec - start_time_.tv_usec) / 1.0e6;return clip_precision(time, 2);
#endif
}

2.2 C++标准库

std::chrono::steady_clock类型

Timer.h

#pragma once#define NOMINMAX
#undef min
#undef max#include <chrono>
#include <stdexcept>/// <summary>
/// 基于C++标准库时间函数,进行计时(支持跨平台使用)
/// </summary>class Timer
{
public:Timer(): m_start(std::chrono::steady_clock::time_point::min()){}void clear(){m_start = std::chrono::steady_clock::time_point::min();}bool isStarted() const{return (m_start != std::chrono::steady_clock::time_point::min());       //最小时间点}void start(){//steady_clock 是一个专门用于测量时间间隔的时钟,//它提供了稳定的、不受系统时间调整影响的时间m_start = std::chrono::steady_clock::now();}std::int64_t getMs() const{if (!this->isStarted()) {throw std::runtime_error("计时器未启动!");}const std::chrono::steady_clock::duration diff = std::chrono::steady_clock::now() - m_start;return std::chrono::duration_cast<std::chrono::milliseconds>(diff).count();}private:std::chrono::steady_clock::time_point m_start;
};

std::clock类型

BasicTimer.h

#ifndef TIMER_H
#define TIMER_H#include <cfloat>/// <summary>
/// 测量用户进程时间的计时器类, 它只计算处于
/// 运行状态(CPU执行)的时间, 时间信息以秒为单位给出。
/// </summary>class BasicTimer
{
public:BasicTimer() : elapsed_(0.0), started_(0.0), interv_(0), running_(false) {}void	start();void	stop();void	reset();bool	is_running() const { return running_; }double	time()       const;int		intervals()  const { return interv_; }double	precision()  const;private:double	user_process_time()     const; //秒double	compute_precision() const; //秒double	elapsed_;double	started_;int		interv_;bool	running_;static bool failed_;
};#endif

BasicTimer.cpp

#include "BasicTimer.h"#include <climits>
#include <ctime>
#include <cfloat>
#include <assert.h>
#include <cmath>bool BasicTimer::failed_ = false;template <class T>
inline T basic_timer_clip_precision(T v, int p) {float tmp = std::pow(10.0f, p);return (static_cast<int>(v * tmp) / tmp);
}double BasicTimer::user_process_time() const {// 与操作系统相关。 返回一个单调增加的时间(以秒为单位)//(在溢出的情况下可能会回绕,请参阅 max()),// 如果该时间的系统调用失败,则返回 0.0。 // 如果系统调用失败,则设置静态标志“m_failed”。std::clock_t clk = std::clock();assert(clk != (std::clock_t)-1);if (clk != (std::clock_t)-1) {return double(clk) / CLOCKS_PER_SEC;}else {failed_ = true;return 0.0;}
}double BasicTimer::compute_precision() const {// 动态计算计时器精度(以秒为单位)。double min_res = DBL_MAX;for (int i = 0; i < 5; ++i) {double current = user_process_time();if (failed_)return -1.0;double next = user_process_time();while (current >= next) {		// 等到计时器增加next = user_process_time();if (failed_)return -1.0;}// 获取所有运行的最小时间差。if (min_res > next - current)min_res = next - current;}return min_res;
}double BasicTimer::precision() const {// 如果计时器系统调用失败,则第一次调用时计算精度返回 -1.0。static double prec = compute_precision();return prec;
}void BasicTimer::start() {assert(!running_);started_ = user_process_time();running_ = true;++interv_;
}void BasicTimer::stop() {assert(running_);double t = user_process_time();elapsed_ += (t - started_);started_ = 0.0;running_ = false;
}void BasicTimer::reset() {interv_ = 0;elapsed_ = 0.0;if (running_) {started_ = user_process_time();++interv_;}else {started_ = 0.0;}
}double BasicTimer::time() const {if (running_) {double t = user_process_time();return basic_timer_clip_precision(elapsed_ + (t - started_), 6);}return basic_timer_clip_precision(elapsed_, 6);
}

三、实现效果

测试:

#include <iostream>#include "../StopWatch.h"
#include "../Timer.h"
#include "../BasicTimer.h"int main()
{// ------------------------windows平台----------------------StopWatch stopWatch;stopWatch.start();// --------------------C++标准库(支持跨平台)---------------Timer timer;timer.start();BasicTimer basicTimer;basicTimer.start();for (int i = 0; i < 10000000; ++i){//循环,用于计时}// ------------------------输出结果----------------------std::cout << "StopWatch 计时为:" << stopWatch.elapsed() << "s" << std::endl;std::cout << "Timer 计时为:" << timer.getMs() << "ms" << std::endl;std::cout << "BasicTimer 计时为:" << basicTimer.time() << "s" << std::endl;return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1424830.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

C++ static_cast学习

static_cast可实现&#xff0c; 1 基本类型之间的转换 2 void指针转换为任意基本类型的指针 3 用于有继承关系的子类与父类之间的指针或引用的转换 用于基本类型转化时&#xff0c;会损失精度类似于C语言的强制转化&#xff1b; 下面先看一下void指针的转换&#xff1b; …

CSS学习笔记之中级教程(二)

-.CSS学习笔记之中级教程&#xff08;一&#xff09; 6、CSS 布局 - display: inline-block 与 display: inline 相比&#xff0c;主要区别在于 display: inline-block 允许在元素上设置宽度和高度。 同样&#xff0c;如果设置了 display: inline-block&#xff0c;将保留上下…

【数据库】知识总结(期末复习)

题型&#xff1a; 一、选择题(共10题&#xff0c;每题2分&#xff0c;共20分) 二、填空题(共10空&#xff0c;每空1分&#xff0c;共10分) 三、关系代数计算题&#xff08;共5题&#xff0c;每题2分&#xff0c;共10分&#xff09; 四、SQL计算题(共10题&#xff0c;每题3分…

C++ | Leetcode C++题解之第92题反转链表II

题目&#xff1a; 题解&#xff1a; class Solution { public:ListNode *reverseBetween(ListNode *head, int left, int right) {// 设置 dummyNode 是这一类问题的一般做法ListNode *dummyNode new ListNode(-1);dummyNode->next head;ListNode *pre dummyNode;for (i…

东莞酷得电子方案 遥控水弹坦克车

首先遥控小车是一种能够通过无线遥控器进行远程操控的小型机器人。遥控小车应用了哪些软硬件技术呢&#xff1f;本文将从以下几个方面进行详细介绍。 遥控小车应用了多种软硬件技术&#xff0c;涉及底盘结构、动力系统、传感器、控制器等多个方面。 底盘结构&#xff1a;遥控…

PostgreSQL扩展之PGroonga:多语言全文搜索

简介 PGroonga 是一个 PostgreSQL 扩展&#xff0c;它增加了基于 Groonga 的全文搜索索引方法。虽然原生的 PostgreSQL 支持全文索引&#xff0c;但它仅限于基于字母和数字的语言。PGroonga 提供了更广泛的字符支持&#xff0c;使其成为 PostgreSQL 支持的语言的超集&#xff…

【考研数学】张宇《1000题》强化阶段正确率多少算合格?

张宇1000题真的很练人心态.... 基础不好&#xff0c;建议别碰1000题 基础好&#xff0c;1000题建议在两个月以内刷完 如果自己本身在基础阶段学的比较水&#xff0c;自己的薄弱点刷了一小部分题没有针对性完全解决&#xff0c;转身去刷1000题就会发现&#xff0c;会的题目刷…

一线互联网大数据面试题核心知识库(100万字)

本面试宝典涵盖大数据面试高频的所有技术栈&#xff0c;包括Liunx&Shell基础&#xff0c;Hadoop&#xff0c;Zookpeer&#xff0c;Flume&#xff0c;Kafka&#xff0c;Hive&#xff0c;Datax&#xff0c;Maxwell&#xff0c;DolphinScheduler&#xff0c;Spark Core&SQ…

王炸!OpenAI全新模型GPT-4o推出!免费使用,实时语音视频交互来了!

北京时间5月14日凌晨&#xff0c;OpenAI 春季新品发布会举行&#xff0c;新一代旗舰生成模型 GPT-4o来了。GPT-4o 的推出代表着技术进步的一大步&#xff0c;集成了文本、语音和图像三种模态&#xff0c;使人机交互更加自然和高效。 这样的话&#xff0c;目前可以使用的版本包括…

[Linux][网络][高级IO][三][IO多路转接][epoll]详细讲解

目录 1.IO多路转接之epoll1.epoll初识2.epoll_create()3.epoll_ctl()4.epoll_wait()5.epoll工作原理6.epoll使用过程三部曲7.epoll的优点(和select缺点对应)8.思考 && 问题 2.epoll工作方式0.感性理解 && 铺垫1.水平触发(Level Triggered)工作模式2.边缘触发(E…

webpack优化构建速度示例-externals:

externals 配置项主要用于防止将某些 import 的包&#xff08;package&#xff09;打包到 bundle 中&#xff0c;而是在运行时&#xff08;runtime&#xff09;再从外部获取这些扩展依赖&#xff08;external dependencies&#xff09;。这样做的主要目的是为了解决打包文件过大…

cypress的安装使用

cypress npm install -g cnpm --registryhttps://registry.npm.taobao.org cypress的启动打开 npx cypress open js函数的回调 function print(string,callback){console.log(string)callback() } print("a",function(){print("b",function(){console.l…

spark知识点

目录 第二章Scala基础 一.Scala常用数据类型 二.定义与使用数组 三.定义与使用函数 四.定义与使用列表 五.定义与使用集合 六.定义与使用映射 七.定义与使用元组 第三章Spark编程基础 一.从内存中读取创建RDD 二.从外部存储系统中读取数据创建RDD 三.RDD方法归纳 1.…

Qt+C++串口调试工具

程序示例精选 QtC串口调试工具 如需安装运行环境或远程调试&#xff0c;见文章底部个人QQ名片&#xff0c;由专业技术人员远程协助&#xff01; 前言 这篇博客针对《QtC串口调试工具》编写代码&#xff0c;代码整洁&#xff0c;规则&#xff0c;易读。 学习与应用推荐首选。 …

异步编程CompletableFuture总结

文章目录 1. 简介&#xff1a;2. 比较1、传统方式2、使用CompletableFuture&#xff1a;异步执行返回值 3、组合处理&#xff1a;anyOfallof : 4. 异步回调&#xff1a;thenAcceptthenApplywhenComplete等同于 thenAccepthandel()等同于thenApply 5. 常用方法&#xff1a;1、su…

Vue的学习 —— <路由与网络请求>

目录 前言 正文 一、初识路由 二、初识Vue Router 1、安装Vue Router 2、Vue Router基本使用 三、路由重定向 四、嵌套路由 前言 在之前的学习中了解到单页Web应用通常只有一个HTML页面&#xff0c;所有的组件展示和切换都在这个页面上完成。虽然我们可以通过动态组件…

基于CentOS-7搭建hadoop3.3.6大数据集群(保姆级教程)

目录 安装虚拟机 为hadoop用户添加权限 关闭防火墙 修改主机名以及ip地址映射 配置ip 连接xshell &#xff0c;以hadoop用户登录 创建目录并将该文件夹权限赋予hadoop用户 安装配置jdk 关闭虚拟机&#xff0c;克隆其他两个节点 修改主机名和ip地址 配置免密登录 安装…

基于Vue和uni-app的增强型多选框Checkbox组件开发

基于Vue和uni-app的增强型多选框&#xff08;Checkbox&#xff09;组件开发 摘要 随着前端技术的不断发展和用户体验要求的提升&#xff0c;传统的Checkbox组件已不能满足所有需求。本文介绍了一种基于Vue和uni-app的增强型多选框&#xff08;Checkbox&#xff09;组件的开发…

C语言 | Leetcode C语言题解之第91题解码方法

题目&#xff1a; 题解&#xff1a; int numDecodings(char* s) {int n strlen(s);// a f[i-2], b f[i-1], c f[i]int a 0, b 1, c;for (int i 1; i < n; i) {c 0;if (s[i - 1] ! 0) {c b;}if (i > 1 && s[i - 2] ! 0 && ((s[i - 2] - 0) * 10…

程序员的实用神器之——通义灵码

通义灵码介绍 “通义灵码”是一款基于阿里云通义代码大模型打造的智能编码助手&#xff0c;产品于2023年10月31日云栖大会上&#xff0c;正式对外发布。新手亦能驾轻&#xff0c;老手恒常运&#xff0c;唯手熟尔。 通义灵码产品介绍_智能编码助手_AI编程_云效(Apsara Devops)…