steady_clock

public static member function
<chrono>

std::chrono::steady_clock::now

static time_point now() noexcept;
获取当前时间
返回 steady_clock 参照系下的当前 time_point

参数



返回值

表示当前时间的 time_point
time_point是一个成员类型,定义为time_point<steady_clock>.

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// steady_clock example
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>

int main ()
{
  using namespace std::chrono;

  steady_clock::time_point clock_begin = steady_clock::now();

  std::cout << "printing out 1000 stars...\n";
  for (int i=0; i<1000; ++i) std::cout << "*";
  std::cout << std::endl;

  steady_clock::time_point clock_end = steady_clock::now();

  steady_clock::duration time_span = clock_end - clock_begin;

  double nseconds = double(time_span.count()) * steady_clock::period::num / steady_clock::period::den;

  std::cout << "It took me " << nseconds << " seconds.";
  std::cout << std::endl;

  return 0;
}

可能的输出
printing out 1000 stars...
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
****************************************
It took me 0.084003 seconds.


另见