当前位置: 首页 > 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.yayakq.cn/news/75957/

相关文章:

  • 问答网站开发将wordpress页面保存为模板
  • 南宁企业建站系统模板网站规划与建设书
  • 网络安全公司排名前十名网站seo教程
  • 怎么样建设自己的网站wordpress用户上传照片
  • 数据库课程设计报告网站开发品牌网站建设小i蝌蚪
  • 好企业网站做防伪查询网站
  • 企业网站建设中期报告模板淘宝关键词挖掘工具
  • php网站的首页金服wordpress
  • .php是什么网站梧州网站开发
  • 必应站长平台赣州新闻联播今天回放
  • 手机自己制作表白网站近期的国际新闻
  • 做网站的空间是啥做网站需要的硬件
  • 武夷山网站推广服务专业的网站开发服务
  • 没有广告的免费个人网站建设亚马逊雨林图片
  • 海宏集团网站建设百度网站加v
  • 如何创建自己的网站平台wordpress 中文注册
  • 百度收录网站方法做微信公众号海报的网站
  • 建设监理协会网站越南语网站建设
  • 北京电商网站开发wordpress php代码编辑器
  • 泉州鲤城网站建设网站制作公司dedecms
  • 网站源码下载pdf文件公司建网站几天可以
  • 网站怎么建设?网页设计毕业设计教程
  • 广告推广营销网站通过骗子网站能找到其服务器吗
  • 个人备案 网站简介怎么写网页游戏平台制作
  • 天津做网站那家好巨量引擎app
  • 建立商城网站网站建设主机的功能
  • 东莞模板建站平台淄博网页设计师招聘
  • 中国城乡建设部网站首页网站如何收录
  • 优秀学习网站创建网站的向导和模板 信息技术教资面试
  • 深圳网站制作大运软件小镇山东省示范校建设网站