Created
May 25, 2015 17:02
-
-
Save ra1u/57a6cb43f6a22134959e to your computer and use it in GitHub Desktop.
gcc tight loop measure
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 <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 | |
std::atomic_int_fast64_t g_milliseconds(0); | |
static int runclock() { | |
auto timeout = std::chrono::high_resolution_clock::now(); | |
while (1) { | |
std::this_thread::sleep_for(1ms); | |
auto now = std::chrono::high_resolution_clock::now(); | |
while (timeout < now) { | |
g_milliseconds++; | |
timeout += 1ms; | |
} | |
} | |
} | |
static std::uint_fast64_t get_ms_time() { return g_milliseconds; } | |
int main() { | |
std::thread t1(runclock); | |
uint_fast64_t times = 0; | |
while (get_ms_time() == 0) { | |
// wait timer to start running | |
std::this_thread::sleep_for(1ms); | |
} | |
auto lastTime = get_ms_time(); | |
while (1) { | |
auto now = get_ms_time(); | |
if (now <= lastTime) { | |
times++; | |
} else { | |
std::cout << times << "\r"; | |
times = 0; | |
lastTime = now; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment