Skip to content

Instantly share code, notes, and snippets.

@toshok
Created April 29, 2015 16:50
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 toshok/47e993196048a8261026 to your computer and use it in GitHub Desktop.
Save toshok/47e993196048a8261026 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <chrono>
#include <x86intrin.h>
#include <unistd.h>
struct rdtsc_clock {
typedef unsigned long long rep;
typedef std::ratio<1, 2800098000> period; // /proc/cpuinfo says 2800.098 for cpu MHz
typedef std::chrono::duration<rep, period> duration;
typedef std::chrono::time_point<rdtsc_clock> time_point;
static const bool is_steady = true;
static time_point now() noexcept
{
return time_point(duration(static_cast<rep>(__rdtsc())));
}
};
int
main()
{
rdtsc_clock::time_point start = rdtsc_clock::now();
sleep(10);
rdtsc_clock::time_point end = rdtsc_clock::now();
printf ("%ld ms\n", std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment