C++11 中运行代码块耗时的方法以及坑(chrono 方法)
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 方法)的更多相关文章
- 【Java基础】2、Java中普通代码块,构造代码块,静态代码块区别及代码示例
Java中普通代码块,构造代码块,静态代码块区别及代码示例.Java中普通代码块,构造代码块,静态代码块区别及代码示例 执行顺序:静态代码块>静态方法(main方法)>构造代码块>构 ...
- Java中普通代码块,构造代码块,静态代码块的代码演示样例及区分
//运行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 当中静态代码块仅仅运行一次.构造代码块在每次创建对象是都会运行. 1 普通代码块 <span ...
- 【Java学习笔记之十七】Java中普通代码块,构造代码块,静态代码块区别及代码示例分析
//执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 其中静态代码块只执行一次.构造代码块在每次创建对象是都会执行. 1 普通代码块 //普通代码块:在 ...
- java中普通代码块,构造代码块,静态代码块的区别及代码示例
本文转自:http://www.cnblogs.com/sophine/p/3531282.html 执行顺序:(优先级从高到低)静态代码块>main方法>构造代码块>构造方法. 其 ...
- 面向对象(Java中普通代码块,构造代码块,静态代码块区别及代码示例)
//执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 其中静态代码块只执行一次.构造代码块在每次创建对象是都会执行. 1 普通代码块 //普通代码块:在 ...
- java中静态代码块,非静态代码块,构造函数
关于静态代码块 静态代码块的写法: static { System.out.println("我是静态代码块"); } 静态代码块的特点: 1.执行优先级高于非静态的初始化块,它会 ...
- java 子类、父类中静态代码块、字段,非静态代码块、字段以及构造函数的初始化顺序和次数
一个类中的数据初始化顺序是面试官非常喜欢出的面试题之一,本文用一个实例来介绍java中子类.父类中静态代码块.字段,非静态代码块.字段以及构造函数的执行顺序和次数. 一.包结构
- Java 中静态代码块初始化问题测试
Java 中静态代码块初始化问题测试 原创 情况一:变量是 static final 修饰的"编译期常量",如 public static final String a = &qu ...
- Java中四大代码块的运行顺序(附code)
验证证的方法是写code.例如以下: public class test { static class A { public static String name = "hello" ...
- Java中普通代码块,构造代码块,静态代码块区别及代码示例
//执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 其中静态代码块只执行一次.构造代码块在每次创建对象是都会执行. 1 普通代码块 1 //普通代码块 ...
随机推荐
- Proxmox的local-lvm改文件存储,提升运行速度
介绍 Proxmox的缺省安装会创建 local 和 local-lvm 两个存储.其中local大约磁盘容量的10%,存储类别为目录. local-lvm的存储类别为 lvm-thin. 实际使用中 ...
- 素数打表,洛谷P1217 [USACO1.5]回文质数 Prime Palindromes
这道题的最后一个样例TLE(超时)了,判断素数的条件是 i*i<n 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include ...
- vue 导出多页pdf, window.print()实现
如果你对分页打印没思路,而网上的现成方案又不适合,不妨进来看看,也许会对你有帮助. 由于工作环境是局域网,对于插件的安装有限制,所以排除了jspdf + html2canvas的实现方式:采用wind ...
- NC23051 华华和月月种树
题目链接 题目 题目描述 华华看书了解到,一起玩养成类的游戏有助于两人培养感情.所以他决定和月月一起种一棵树.因为华华现在也是信息学高手了,所以他们种的树是信息学意义下的. 华华和月月一起维护了一棵动 ...
- NC23482 小A的最短路
题目链接 题目 题目描述 小A这次来到一个景区去旅游,景区里面有N个景点,景点之间有N-1条路径.小A从当前的一个景点移动到下一个景点需要消耗一点的体力值.但是景区里面有两个景点比较特殊,它们之间是可 ...
- 修改文件权限后Git 文件目录被标记为修改
刚打开IDE,工作区的代码状态全部变成修改未提交的状态了?这是这么回事?这是因为Git忽略文件权限或者拥有者改变导致的git状态变化.默认Git会记录文件的权限信息,如果文件的权限信息被修改,在Git ...
- 【Unity3D】基于深度和法线纹理的边缘检测方法
1 前言 边缘检测特效中使用屏后处理技术,通过卷积运算计算梯度,检测每个像素周围像素的亮度差异,以识别是否是边缘像素:选中物体描边特效中也使用了屏后处理技术,通过 CommandBuffer 获取 ...
- win32 - ListView_GetItemPosition的使用
ListView_GetItemPosition : Gets the position of a list-view item 理论上获得桌面图标的正确方法是使用shell项,=> IFold ...
- go语言编程常见问题
在Goland中运行单元测试报错Error: Cannot find package 如下图,在Goland中运行单元测试时报错:"Error: Cannot find package&qu ...
- 项目实战:Qt+OpenCV图像处理与识别算法平台
若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...