How To Get The Current CPU Cycle Count

With a little bit of help from assembler, we can find the exact CPU cycle count. This can be very much helpful to find the execution time of a program or function. The code to find the cpu cycle count is given below.

 
__int64 GetCpuCycle()
{
   unsigned int LowWord = 0;
   unsigned int HighWord = 0;
   _asm
   {
      // Insert Real Time Stamp Counter opcodes
      _emit 0x0F
      _emit 0x31
      mov HighWord, edx
      mov LowWord, eax
   }
   return ((__int64)(HighWord) << 32) + LowWord; } [/sourcecode] We can use the above function at the beginning and end of a program to get the no. of clock cycles it needed.

Advertisements

2 Responses to How To Get The Current CPU Cycle Count

  1. Pete says:

    Is this supposed to compile easily? I’ve been learning a lot about assembler trying to make this compile on my windows machine.

  2. shiplu says:

    will it return same for specific code?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: