PS:要转载请注明出处,本人版权所有。

PS: 这个只是基于《我自己》的理解,

如果和你的原则及想法相冲突,请谅解,勿喷。

前置说明

  本文作为本人csdn blog的主站的备份。(BlogID=057)

  本文发布于 2018-03-19 11:18:21,现用MarkDown+图床做备份更新。blog原图已丢失,使用csdn所存的图进行更新。(BlogID=057)

环境说明

  无

前言


  无

chrono


chrono 简介:

  来源:http://www.cplusplus.com/reference/chrono/?kw=chrono

The elements in this header deal with time. This is done mainly by means of three concepts:
Durations
They measure time spans, like: one minute, two hours, or ten milliseconds.
In this library, they are represented with objects of the duration class template, that couples a count representation and a period precision (e.g., ten milliseconds has ten as count representation and milliseconds as period precision).
Time points
A reference to a specific point in time, like one's birthday, today's dawn, or when the next train passes.
In this library, objects of the time_point class template express this by using a duration relative to an epoch (which is a fixed point in time common to all time_point objects using the same clock).
Clocks
A framework that relates a time point to real physical time.
The library provides at least three clocks that provide means to express the current time as a time_point: system_clock, steady_clock and high_resolution_clock.
对我来看:

  3种计时方式以及一个时间转换方式就ok,分别对应:

计时方式:
std::chrono::high_resolution_clock //high_resolution_clock is the clock with the shortest tick period. It may be a synonym for system_clock or steady_clock.
std::chrono::system_clock //Specifically, system_clock is a system-wide realtime clock.
std::chrono::steady_clock //steady_clock is specifically designed to calculate time intervals.
时间转换方式:
std::chrono::duration_cast
/*
Converts the value of dtn into some other duration type, taking into account differences in their periods. The function does not use implicit conversions. Instead, all count values are internally converted into the widest representation (the common_type for the internal count types) and then casted to the destination type, all conversions being done explicitly with static_cast.
*/
chrono 大法中的坑(不同的时钟计时存在ms及us及ns级别的差别)
//windows下,std::chrono::system_clock 可能计时不准确,注意这里是“”“”可能“”“”
//windows下,high_resolution_clock 时间计时最精确,如果需要最高精确度,请使用此时钟
//总结:
//1 如果是秒级时间评估,std::chrono::system_clock 无所谓,如果是ms、us、ns级的时间评估(这里我做算法评估),那么std::chrono::system_clock可能会出现较大的问题,这时候可用high_resolution_clock来解决此问题。
//2 如果计时应用在了多个线程,你需要把所有线程的计时加起来,才是你的真正耗时
//同时我猜测,出现此问题的原因就是不同时钟类型用的时间片(就是对应不同类型的一个时钟周期)是不一样的。当然,有一些cpu相关知识的朋友应该知道,最准确的计时是:时间片=cpu时钟周期(这是不合理的,理解到一部分就行了)
chrono 计时方法
//代码点一
std::chrono::time_point<std::chrono::high_resolution_clock> p0 = std::chrono::high_resolution_clock::now();
// ....... 若干代码
//代码点二
std::chrono::time_point<std::chrono::high_resolution_clock> p1 = std::chrono::high_resolution_clock::now(); //计算及打印耗时,用法不太标准,文中的1000 是换算到毫秒的意思
cout << "stitch high_resolution_clock time:" << (float)std::chrono::duration_cast<std::chrono::microseconds>(p1 - p0).count() / 1000 << "ms" << endl;

后记


  无

参考文献


打赏、订阅、收藏、丢香蕉、硬币,请关注公众号(攻城狮的搬砖之路)

PS: 请尊重原创,不喜勿喷。

PS: 要转载请注明出处,本人版权所有。

PS: 有问题请留言,看到后我会第一时间回复。

C++11 中运行代码块耗时的方法以及坑(chrono 方法)的更多相关文章

  1. 【Java基础】2、Java中普通代码块,构造代码块,静态代码块区别及代码示例

    Java中普通代码块,构造代码块,静态代码块区别及代码示例.Java中普通代码块,构造代码块,静态代码块区别及代码示例 执行顺序:静态代码块>静态方法(main方法)>构造代码块>构 ...

  2. Java中普通代码块,构造代码块,静态代码块的代码演示样例及区分

    //运行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 当中静态代码块仅仅运行一次.构造代码块在每次创建对象是都会运行. 1 普通代码块 <span ...

  3. 【Java学习笔记之十七】Java中普通代码块,构造代码块,静态代码块区别及代码示例分析

    //执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 其中静态代码块只执行一次.构造代码块在每次创建对象是都会执行. 1 普通代码块 //普通代码块:在 ...

  4. java中普通代码块,构造代码块,静态代码块的区别及代码示例

    本文转自:http://www.cnblogs.com/sophine/p/3531282.html 执行顺序:(优先级从高到低)静态代码块>main方法>构造代码块>构造方法. 其 ...

  5. 面向对象(Java中普通代码块,构造代码块,静态代码块区别及代码示例)

    //执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 其中静态代码块只执行一次.构造代码块在每次创建对象是都会执行. 1 普通代码块 //普通代码块:在 ...

  6. java中静态代码块,非静态代码块,构造函数

    关于静态代码块 静态代码块的写法: static { System.out.println("我是静态代码块"); } 静态代码块的特点: 1.执行优先级高于非静态的初始化块,它会 ...

  7. java 子类、父类中静态代码块、字段,非静态代码块、字段以及构造函数的初始化顺序和次数

    一个类中的数据初始化顺序是面试官非常喜欢出的面试题之一,本文用一个实例来介绍java中子类.父类中静态代码块.字段,非静态代码块.字段以及构造函数的执行顺序和次数. 一.包结构

  8. Java 中静态代码块初始化问题测试

    Java 中静态代码块初始化问题测试 原创 情况一:变量是 static final 修饰的"编译期常量",如 public static final String a = &qu ...

  9. Java中四大代码块的运行顺序(附code)

    验证证的方法是写code.例如以下: public class test { static class A { public static String name = "hello" ...

  10. Java中普通代码块,构造代码块,静态代码块区别及代码示例

    //执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 其中静态代码块只执行一次.构造代码块在每次创建对象是都会执行. 1 普通代码块 1 //普通代码块 ...

随机推荐

  1. 大数据技术之DataX

    一.DataX简介 DataX 是阿里巴巴开源的一个异构数据源离线同步工具,致力于实现包括关系型数据库(MySQL.Oracle等).HDFS.Hive.ODPS.HBase.FTP等各种异构数据源之 ...

  2. UVA12390 Distributing Ballot Boxes 题解

    题目传送门 题意 有 \(n\) 个城市,\(b\) 个投票箱,第 \(i\) 个城市有 \(a_i\) 人,每个人均有一张票,将 \(b\) 个投票箱分给 \(n\) 个城市,每个城市的票分摊在投票 ...

  3. 计网学习笔记十 Internet Routing Protocols

    上一讲简单介绍了routing和两种最小路径算法的使用,这节课讲了两种算法是如何糅合进协议中来实现具体使用的. 这一讲内容有AS的介绍,以及IGP和BGP协议. Autonomous Systems ...

  4. NC15065 小牛vs小客

    题目链接 题目 题目描述 小牛和小客玩石子游戏,他们用n个石子围成一圈,小牛和小客分别从其中取石子,谁先取完谁胜,每次可以从一圈中取一个或者相邻两个,每次都是小牛先取,请输出胜利者的名字(小牛获胜输出 ...

  5. python常用的搜索字符内容函数详解:re.findall/findfiter

    区别findall返回listfinditer返回一个MatchObject类型的iterator详细举例介绍1.findall在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹 ...

  6. win10下开启IIS部署网站

    今天测试一个有关IIS的安全漏洞用了下win10下的IIS,记录一下,供网友参考. 1.启用IIS 依次点开: 控制面板-----程序----启用或关闭Windows功能 以下图片中打钩的地方全勾上. ...

  7. webrtc 的理解

    常规视频的传输包括以下几个步骤:采集,编码,推流,转码,分发,拉流,解码和渲染 在一个实时的音视频系统架构里,上面的每个环节都会有一定程度的优化空间. 以下内容摘自:rtmp直播和webrtc直播对比 ...

  8. windbg 学习

    常用的 windbg 命令 .ecxr 用来切换到异常发生时的上下文,主要用在分析静态 dump 文件的时候.当我们使用 .reload 命令去强制加载库的 pdb 文件后,需要执行 .ecxr 命令 ...

  9. 在vue项目中使用scss语法的准备步骤

    在vue项目中使用scss语法的准备步骤 个人总结: 在项目根目录cmd控制台中使用以下命令行,安装vue项目中使用scss的相关依赖; 在["项目根目录/build/webpack.bas ...

  10. maven引入本地jar不能打入部署包的问题解决

    引入的三方依赖 jar 包, scope 为 system 的包 maven 默认是不打包进去的,需要加这个配置 在pom.xml文件中找到spring-boot-maven-plugin插件,添加如 ...