Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
test sleep precision
// Copyright (c) 2018 spockwang.
// All rights reserved.
//
// Author: wbbtiger@gmail.com
//
#include <chrono>
#include <iostream>
#include <thread>
using namespace std;
int main()
{
int wait_micros = 100;
int cnt = 10*10000000 / wait_micros;
auto start_time = std::chrono::steady_clock::now();
for (int i = 0; i < cnt; ++i) {
std::this_thread::sleep_for(std::chrono::microseconds(wait_micros));
}
auto end_time = std::chrono::steady_clock::now();
auto duration_micros = std::chrono::duration_cast<std::chrono::microseconds>(end_time-start_time).count();
cout << "avg micros=" << (duration_micros/cnt) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment