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. 使用私有gitlab搭建gitbook持续集成

    目录 环境搭建 1. 安装 Node.js 2. 安装 gitbook 3. 安装 Gitlab Runner 4. 注册Runner gitbook 配置 1. 目录结构 2. 命令行 3. 插件 ...

  2. spring声明式事务(@Transactional)开发常犯的几个错误及解决办法

    spring声明式事务(@Transactional)开发常犯的几个错误及解决办法 目前JAVA的微服务项目基本都是SSM结构(即:springCloud +springMVC+Mybatis),而其 ...

  3. idea启动springboot项目报错java.lang.ClassNotFoundException: com.gctl.bpm.GctlBpmApplication解决方案

    有时候父子工程改造springboot项目时会报错java.lang.ClassNotFoundException: com.gctl.bpm.GctlBpmApplication如下图所示 此时不要 ...

  4. NC14402 求最大值

    题目链接 题目 题目描述 给出一个序列,你的任务是求每次操作之后序列中 (a[j]-a[i])/(j-i)[1<=i<j<=n]的最大值. 操作次数有Q次,每次操作需要将位子p处的数 ...

  5. pip指定镜像安装

    清华大学开源软件镜像站

  6. Swoole从入门到入土(4)——TCP服务器[正确重启]

    在上一篇中,我们提到了一个配置项max_wait_time.这个配置项决定了在服务端在进程经束的时候,在max_wait_time时间内onWorkerStop事件会完成扫尾工作. 那什么时候work ...

  7. 《系列二》-- 8、单例bean的创建

    目录 1 源码入口概述 2 getSingleton(beanName, ObjectFactory) 的行为 总结 阅读之前要注意的东西:本文就是主打流水账式的源码阅读,主导的是一个参考,主要内容需 ...

  8. 《深入理解Java虚拟机》(六) 调优策略 -- 笔记

    目录 一.操作文档类功能,大量大对象直接进入老年代 问题现象 解决方法 通过单一Java虚拟机管理大量的内存 同一台服务器上部署若干Java虚拟机 二.异步请求大量累积 三.排查问题 排查问题: 可能 ...

  9. centos上使用makefile编译sliver时 提示gcc 错误,cannot find -ldl cannot find -lpthread cannot find -lc

    github.com/bishopfox/sliver/server /usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit ...

  10. RK3568开发笔记(三):RK3568虚拟机基础环境搭建之更新源、安装网络工具、串口调试、网络连接、文件传输、安装vscode和samba共享服务

    前言   开始搭建RK3568的基础虚拟机,具备基本的通用功能,主要包含了串口工具minicom,远程登陆ssh,远程传输filezilla,代码编辑工具vscode.   虚拟机   文档对对虚拟机 ...