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

2019-12-02  本文已影响0人  阿群1986

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:

 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;
}

参考

上一篇 下一篇

猜你喜欢

热点阅读