Skip to content

Instantly share code, notes, and snippets.

@ra1u
Created May 25, 2015 19:01
Show Gist options
  • Save ra1u/06f6a52db67082b293c4 to your computer and use it in GitHub Desktop.
Save ra1u/06f6a52db67082b293c4 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <chrono>
#include <thread>
#include <atomic>
using namespace std::literals;
// compile command
// g++ -lpthread -std=c++14 -march=native -O3 timeTest.cpp -o timeTest
// use thread safe increment this value each ms
typedef std::atomic_int_fast64_t clocktype;
clocktype g_tick_counter(0);
auto const one_Tick = std::chrono::milliseconds(1000);
static int runclock() {
auto timeout = std::chrono::high_resolution_clock::now();
while (1) {
std::this_thread::sleep_for(one_Tick);
auto now = std::chrono::high_resolution_clock::now();
while (timeout < now) {
g_tick_counter++;
timeout += one_Tick;
}
}
}
static std::uint_fast64_t get_ms_time() { return g_tick_counter; }
int main() {
std::thread t1(runclock);
uint64_t times = 0;
uint64_t slowcounter = 0;
while (get_ms_time() == 0) {
// wait timer to start running
std::this_thread::sleep_for(1ms);
}
auto lastTime = get_ms_time();
while (1) {
times += 1;
auto now = get_ms_time();
if (now > lastTime) {
slowcounter += 1;
std::cout << times / slowcounter /
std::chrono::duration_cast<std::chrono::milliseconds>(
one_Tick).count() << " ops per ms" << std::endl;
lastTime = now;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment