Created
October 23, 2014 16:47
-
-
Save socantre/5cb3b2f4df881c11b209 to your computer and use it in GitHub Desktop.
see what resolution a chrono clock has
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <future> | |
#include <atomic> | |
#include <iostream> | |
#include <vector> | |
#include <chrono> | |
template<typename Clock> | |
typename Clock::duration | |
check_clock_resolution() { | |
auto s = Clock::now(); | |
auto e = s; | |
//int calls = 0; | |
while (true) { | |
e = Clock::now(); | |
//++calls; | |
if (e != s) { | |
return e - s; | |
} | |
} | |
} | |
void check_clock_resolution_with_n_tasks(int task_count) { | |
std::atomic<bool> keep_going = true; | |
std::vector<std::future<void>> tasks; | |
for (int i = 0; i < task_count; ++i) { | |
tasks.push_back(std::async(std::launch::async, [&] { | |
while (keep_going.load(std::memory_order_relaxed)) | |
; | |
})); | |
} | |
std::this_thread::sleep_for(std::chrono::seconds(5)); | |
auto d = check_clock_resolution<std::chrono::high_resolution_clock>(); | |
keep_going.store(false, std::memory_order_relaxed); | |
for (auto &&t : tasks) { | |
t.get(); | |
} | |
std::cout << "With " << task_count << " tasks, the resolution is " << | |
std::chrono::duration_cast<std::chrono::nanoseconds>(d).count() << "ns.\n"; | |
} | |
// #include <Windows.h> | |
// #include <mmsystem.h> | |
int main() { | |
// timeBeginPeriod(1); | |
for (int i = 1; i < 30; ++i) { | |
check_clock_resolution_with_n_tasks(i); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment