让我们得到以下代码,它只是测量std::this_thread::sleep_for20ms调用的持续时间:
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
using namespace std::chrono;
int main()
{
for (int i = 0; i < 20; i++)
{
auto start = steady_clock::now();
this_thread::sleep_for(milliseconds(20));
auto end = steady_clock::now();
duration<double, milli> elapsed = end - start;
cout << "Waited " << elapsed.count() << " ms\n";
}
}
Run Code Online (Sandbox Code Playgroud)
当使用工具集v120(VS2013)编译运行时,我得到了预期的结果,即:
Waited 20.0026 ms
Waited 20.0025 ms
Waited 20.0025 ms
Waited 20.0026 ms
Waited 20.0025 ms
Waited 20.0025 ms
Waited 20.0026 ms
Waited 20.0025 ms …Run Code Online (Sandbox Code Playgroud)