Skip to content

Instantly share code, notes, and snippets.

@spockwangs
Created November 12, 2018 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spockwangs/cf0d578708dd73409629740f5aea2afb to your computer and use it in GitHub Desktop.
Save spockwangs/cf0d578708dd73409629740f5aea2afb to your computer and use it in GitHub Desktop.
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