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

做电子元器件的网站艺术培训学校招生方案

做电子元器件的网站,艺术培训学校招生方案,中南大学双一流建设网站,住房城乡建设行业从业人员文章目录 一、简介二、实现代码2.1 windows平台2.2 C标准库 三、实现效果 一、简介 有时候总是会用到一些计时的操作,这里也整理了一些代码,包括C标准库以及window自带的时间计算函数。 二、实现代码 2.1 windows平台 StopWatch.h #ifndef STOP_WATCH_H…

文章目录

  • 一、简介
  • 二、实现代码
    • 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.tj-hxxt.cn/news/123836.html

相关文章:

  • 娱乐城网站模板晋城网站seo
  • 外贸公司网站模板免费微信广告平台推广
  • 56度卫星有几个成人节目呢搜索引擎优化的英语简称
  • 建设银行北京分行网站营销培训方案
  • 潍坊做网站建设常用的网络营销方式
  • 做游戏网站需要多少钱广告投放数据分析
  • 制服定制合同刷移动端seo软件
  • 新乡专业做网站国际新闻
  • 域名申请备案东莞seo网络优化
  • 网站开发排行杭州seo推广公司
  • 珠海左右创意园网站开发优化公司怎么优化网站的
  • 怎么查看网站收录整站优化seo
  • 邯郸网站开发公司百度广告关键词价格表
  • webpack 网站免费淘宝关键词工具
  • 网站 体系深圳最新新闻事件今天
  • 建筑网校排名前十大品牌企业网站优化的三层含义
  • 家庭厨房做外卖网站投广告的平台有哪些
  • 重庆模板网站哪个好热狗seo外包
  • 家装平台都有哪些seo百度点击软件
  • 安远网站建设互联网运营培训课程
  • 公司官网网站建设想法百度账号出售平台
  • 网站美工色彩搭配网站设计公司
  • 招聘网站可以做两份简历吗如何创建一个自己的网站
  • 公司网站推广的方法网络课程
  • 德阳定制建站网站建设报价软文大全800字
  • 网址导航网站建设短视频培训机构排名
  • 做网站一年费用福建seo关键词优化外包
  • 为什么几年前做的网站视频看不了网络营销策划与推广
  • 企业网站制作建设seo教程论坛
  • 湖南手机网站建设公司网站服务器信息查询