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. 如何修改OSW图表中显示的主机名称

    本次测试的OSW版本:831 有人可能会说这种需求是吃饱了撑的吗,谁没事儿改这个名称干嘛啊? 其实并不是,因为有些生产案例非常典型,分享讲解时也需要配合OSW的趋势图来展示,但是出于保护客户隐私(哪怕 ...

  2. MySQL最左匹配原则

    最左匹配原则都是针对联合索引来说的,那么为什么要使用联合索引呢? 一.为什么要使用联合索引? 1.减少开销. 建一个联合索引(col1,col2,col3),实际相当于建了(col1),(col1,c ...

  3. Windows xp 无法远程连接Windows Server 2008

    -------------------------转载自: Windows XPSP3通过网络级身份验证方式连接Windows Server 2008远程桌面 远程桌面大大方便了大家的日常管理工作,W ...

  4. form表单如何实现ajax提交

    最近在开发一个游戏网关的后台管理系统,总结了下中间碰到的一些问题. 之一就是:form表单如何实现ajax提交? 问题:在使用form表单的时候,一旦点击提交触发submit事件,一般会使得页面跳转, ...

  5. 【CVE-2024-21626】容器逃逸漏洞修复

    哈喽大家好,我是咸鱼. 好久不见,最近有一个很火的 CVE--runc 容器逃逸漏洞.年前的时候我们已经在测试环境进行了相关操作打算年后线上进行修复. 因为今天咸鱼才开工,所以文章也就拖到了现在 漏洞 ...

  6. win32 - 创建带有标准阴影的无边框窗口

    这个框框好像删不掉,就先放这边吧...   #define WIN32_LEAN_AND_MEAN #include <unknwn.h> #include <windows.h&g ...

  7. win32-使用GDI+缩放图像

    滑动鼠标滚轮可以改变图像大小 #include <windows.h> #include <tchar.h> #include <Urlmon.h> // URLD ...

  8. Go 中的反射 reflect 介绍和基本使用

    一.什么是反射 在计算机科学中,反射(英语:reflection)是指计算机程序在运行时(runtime)可以访问.检测和修改它本身状态或行为的一种能力.用比喻来说,反射就是程序在运行的时候能够&qu ...

  9. 使用3-hexo主题时无法正常渲染html代码

    问题描述 在hexo框架中使用3-hexo主题时,会遇到这样一个问题:在markdown中嵌入html代码,这些嵌入的html代码无法正常显示. 原因分析 在使用3-hexo主题时,默认使用主题自带的 ...

  10. vscode自定义运行和调试创建launch.json文件及项目独立配置文件

    1.创建lauch.json文件 2.然后在项目目录中会自动创建.vscode的目录 3.在.vscode目录下创建settings.json项目独立配置文件 4.在settings.json中写入 ...