Created
February 5, 2015 11:39
-
-
Save soachishti/3cc70cebc459c95e1ad9 to your computer and use it in GitHub Desktop.
Get CPU Clock using Assembly Require 50 Cycle to run
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
//://stackoverflow.com/questions/275004/c-timer-function-to-provide-time-in-nano-seconds | |
#include <time.h> | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
inline __int64 GetCpuClocks() | |
{ | |
// Counter | |
struct { __int32 low, high; } counter; | |
// Use RDTSC instruction to get clocks count | |
__asm push EAX | |
__asm push EDX | |
__asm __emit 0fh __asm __emit 031h // RDTSC | |
__asm mov counter.low, EAX | |
__asm mov counter.high, EDX | |
__asm pop EDX | |
__asm pop EAX | |
// Return result | |
return *(__int64 *)(&counter); | |
} | |
int main() | |
{ | |
__int64 t; | |
t = GetCpuClocks(); | |
t = GetCpuClocks() - t; | |
cout << t << " CPU Clocks" << "\n\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment