2

Possible Duplicate:
Getting cpu cycles using RDTSC - why does the value of RDTSC always increase?
Get CPU cycle count?

I want to write C++ code which analyzes sorting algorithms, and I need to know how many processor cycles it takes to sort an array.

Any suggestions on how to do that?

I found this code here:

uint64_t rdtsc(){
    unsigned int lo,hi;
    __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
    return ((uint64_t)hi << 32) | lo;
}

I understand it is inline assembly, could someone explain how it works and how to use it?

I run Linux. My computer is dual core, does that makes a difference?

5
  • Why not just measure the wall time like any normal person would do? Commented Dec 18, 2012 at 20:49
  • If you're using rdtsc, you're not "analyzing" sorting algorithms, you're empirically measuring the performance of specific implementations of those algorithms. Commented Dec 18, 2012 at 20:51
  • 1
    @JanDvorak - Not sure what "normal" person you're talking about; I think a lot of programmers concerned about performance use rdtsc or similar. Commented Dec 18, 2012 at 20:52
  • Please read the answer to Getting cpu cycles using RDTSC - why does the value of RDTSC always increase?. Also, note rdtsc always assumes you are running on the same processor. Commented Dec 18, 2012 at 20:52
  • Jasse's Good's comment solved the case, thanks for suggestions. Commented Dec 18, 2012 at 21:19

1 Answer 1

1

Have you looked at the call clock?

It is documented here, and seems to be what you want.

Sign up to request clarification or add additional context in comments.

3 Comments

Fix me if i'm wrong. Basicly why i whant cpu cycles because it's constant, and clock readings depends on situation.
From the documentation: "Returns the processor time consumed by the program." (ie: not CPU-wide, just specific to that one program)
nice, this changes everything.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.