当前位置: 首页 > news >正文

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.lryc.cn/news/350992.html

相关文章:

  • notepad++ 批量转所有文件编码格式为UTF-8
  • 正点原子[第二期]Linux之ARM(MX6U)裸机篇学习笔记-16讲 EPIT定时器
  • 【只会for循环? 来看下, Nodejs中典型的5种循环方式】
  • Java基础(三)- 多线程、网络通信、单元测试、反射、注解、动态代理
  • WordPress建站公司模板免费下载
  • 金融信贷风控基础知识
  • Web Server项目实战4-服务器编程基本框架和2种高效的事件处理模式
  • 。。。。。
  • RPC原理技术
  • 开源大模型与闭源大模型:技术哲学的较量
  • buuctf的RSA(二)
  • idm软件是做什么的 IDM是啥软件 idm软件怎么下载 idm软件怎么下载
  • 基于springboot+vue的学生考勤管理系统
  • Java——内部类
  • 不用从头训练,通过知识融合创建强大的统一模型
  • 僵尸进程、孤儿进程、守护进程
  • 【工程化】CJS 和 ESM
  • 记录:mac pro 16-inch,2019安装ubuntu双系统
  • WordPress主题 7B2 PRO 5.4.2 免授权开心版源码
  • GPT‑4o普通账户也可以免费用
  • 复制即用!纯htmlcss写的炫酷input输入框
  • 前端 CSS 经典:弧形边框选项卡
  • 前端面试题日常练-day21 【面试题】
  • 几起 Linux 问题解决分享
  • LeetCode题解:9. 回文数,翻转一半数字,JavaScript,详细注释
  • 微博:一季度运营利润9.11亿元,经营效率持续提升
  • Mysql总结1
  • three.js能实现啥效果?看过来,这里都是它的菜(05)
  • innerText和innerHTML的区别
  • O2OA(翱途)开发平台数据统计如何配置?