Skip to content

Instantly share code, notes, and snippets.

@coderodde
Created January 30, 2016 12:56
Show Gist options
  • Save coderodde/84c7596b9136712f80af to your computer and use it in GitHub Desktop.
Save coderodde/84c7596b9136712f80af to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
uint64_t read_time_stamp_counter()
{
uint32_t my_edx;
uint32_t my_eax;
asm ("cpuid\n\t"
"rdtsc\n\t"
"movl %%edx, %0\n\t"
"movl %%eax, %1"
:"=r"(my_edx), "=r"(my_eax)
:
: "%eax", "%edx");
return (((uint64_t) my_edx) << 32) | my_eax;
}
int main(int argc, char** argv) {
uint64_t time_stamp_1 = read_time_stamp_counter();
sleep(1);
uint64_t cycles = read_time_stamp_counter() - time_stamp_1;
printf("Cycles: %llu\n", cycles);
printf("Estimated frequency: %.3f GHz\n", cycles / 1e9);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment