美文网首页
2019-12-02 调用C++高精度时钟 std::chron

2019-12-02 调用C++高精度时钟 std::chron

作者: 阿群1986 | 来源:发表于2019-12-02 10:13 被阅读0次
  • 调用C++高精度时钟 std::chrono::high_resolution_clock的方法

https://solarianprogrammer.com/2012/10/14/cpp-11-timing-code-performance/
The C++11 chrono header file provides three standard clocks that could be used for timing one’s code:

  • std::chrono::system_clock - this is the real-time clock used by the system;
  • std::chrono::high_resolution_clock - this is a clock with the shortest tick period possible on the current system;
  • std::chrono::steady_clock - this is a monotonic clock that is guaranteed to never be adjusted.
 1 #include <iostream>
 2 #include <chrono>
 3 
 4 using namespace std;
 5 
 6 int main(){
 7     cout << "system_clock" << endl;
 8     cout << chrono::system_clock::period::num << endl;
 9     cout << chrono::system_clock::period::den << endl;
10     cout << "steady = " << boolalpha << chrono::system_clock::is_steady << endl << endl;
11 
12     cout << "high_resolution_clock" << endl;
13     cout << chrono::high_resolution_clock::period::num << endl;
14     cout << chrono::high_resolution_clock::period::den << endl;
15     cout << "steady = " << boolalpha << chrono::high_resolution_clock::is_steady << endl << endl;
16 
17     cout << "steady_clock" << endl;
18     cout << chrono::steady_clock::period::num << endl;
19     cout << chrono::steady_clock::period::den << endl;
20     cout << "steady = " << boolalpha << chrono::steady_clock::is_steady << endl << endl;
21 
22     return 0;
23 }

This is the output of the above code on Mac OSX, compiled with clang-4.1:

 1 system_clock
 2 1
 3 1000000
 4 steady = false
 5 
 6 high_resolution_clock
 7 1
 8 1000000000
 9 steady = true
10 
11 steady_clock
12 1
13 1000000000
14 steady = true

On this machine, the steady_clock and high_resolution_clock have the same characteristics, both are non-adjustable clocks with nanosecond precision. On the other hand, the system_clock is adjustable and it has only microsecond precision.

测试usleep()的延时误差

// high_resolution_clock example
// 编译命令: g++ -std=c++11 文件名.cpp
#include <stdio.h>
//#include <ctime>
#include <ratio>
using std::ratio;
#include <chrono>
using std::chrono::high_resolution_clock;
using std::chrono::duration;
using std::chrono::duration_cast;

#include <unistd.h>//usleep(): microsecond-level sleep

int main ()
{
  high_resolution_clock::time_point t1 = high_resolution_clock::now();

  for (int i=0; i<10; ++i) {
    usleep(1);
  }

  high_resolution_clock::time_point t2 = high_resolution_clock::now();

  auto time_span = duration_cast<duration<double, std::ratio<1, 1000>>>(t2 - t1);

  printf("It took me %.6F milliseconds...\n", time_span.count());

  return 0;
}

测试printf()/putchar()执行效率实际耗时

// high_resolution_clock example
#include <stdio.h>//#include <iostream>
//#include <ctime>
#include <ratio>

#include <chrono>
using std::chrono::high_resolution_clock;
using std::chrono::duration;
using std::chrono::duration_cast;

int main ()
{

  high_resolution_clock::time_point t1 = high_resolution_clock::now();

  printf("printing out 1000 stars...\n");
  for (int i=0; i<1000; ++i) {
    putchar('*');
  }
  putchar('\n');

  high_resolution_clock::time_point t2 = high_resolution_clock::now();

  duration<double> time_span = duration_cast<duration<double>>(t2 - t1);

  printf("It took me %.6f seconds...\n", time_span.count());

  return 0;
}

参考

相关文章

  • 2019-12-02 调用C++高精度时钟 std::chron

    调用C++高精度时钟 std::chrono::high_resolution_clock的方法 https://...

  • 2.1线程管理基础

    启动线程 使用C++线程库启动线程可以归结为构造std::thread对象: std::thread可以用可调用(...

  • C++常用类型转换备忘

    std::string ->const char * c++内置数值类型->std::stringcpp11 支持...

  • c++模版笔记(3)

    本篇介绍 本篇继续C++的模版介绍 std::invoke 以一种统一形式调用函数和传递参数: 如果需要返回值, ...

  • C++字符串处理小结

    C++中的字符串类型 常用的C++的字符串类型主要是std::string。它是模板std::basic_stri...

  • C++ 线程类 取值

    std::future 获取线程的返回值 std::package_task 包装可调用对象 std::promi...

  • C++11: 多线程简明指南

    std::async 异步调用一个callable的对象,但是不保证调用时机。可以通过传入std::launch:...

  • 2018-10-23 step

    C++ 11 几个特性的整理 std::future - std::promise 解决的问题 返回值的异步获取:...

  • std::function与std::bind

    1、std::function是可调用对象的包装器,可以实现延时调用。 std::funcion可以将类方法及类强...

  • { 1 }CPP_线程管理的基础

    一,启动线程 1. 使用C++线程库启动线程,即为构造std::thread对象: std::thread构造方法...

网友评论

      本文标题:2019-12-02 调用C++高精度时钟 std::chron

      本文链接:https://www.haomeiwen.com/subject/eosxgctx.html