Jump to content
  • Advertisement
Sign in to follow this  

CPU Cycles

This topic is 4774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to calculate the cycle a function takes to return. This function is an-IN-BLACKBOX kind of question like the log, sqrt and many other functions.

Share this post


Link to post
Share on other sites
Advertisement
The way I typicall do quick and dirty benchmarks is using the rdtsc command. Now this only works on the x86 architecture and not all processors support it. What is returned is the number of clock cycles since the computer was powered on.

Typicall what I do is


int intHigh, intLow;
__asm
{
rdtsc;
mov intLow, eax;
mov intHigh, edx;
}
// Insert some code you which to test
__asm
{
rdtsc;
sub eax, intLow;
sub edx, intHigh;
mov intLow, eax;
mov intHigh, edx;
}

Share this post


Link to post
Share on other sites
You can use RDTSC instruction as pointed out by Rattrap, but don't forget to use some sort of serializable instruction. Modern processors execute instuctions out of order, so RDTSC may not be executed when you think it should.
For serialization you can use for example CPUID instruction. But don't forget to execute it few times on the beginning of program, because first few executions are slower.
You can use this to get CPU TSC counter (64 bit value):

inline unsigned __int64 GetTSC()
{
__asm
{
cpuid
rdtsc
}
}

So to test some code you use

unsigned __int64 start, end, cycles;

start = GetTSC();
... code to test ...
end = GetTSC();

cycles = end - start;

This has one drawback - it will return more cycles, because it will also count CPUID instruction, so you will need to measure time it takes to do CPUID and then subtract it from result. If you only need to compare two various functions, it's not needed because in all test same value will be added to results.
HTH

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!