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?

Community
  • 1
  • 1
Ben
  • 3,989
  • 9
  • 48
  • 84
  • Why not just measure the wall time like any normal person would do? – John Dvorak Dec 18 '12 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. – phonetagger Dec 18 '12 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. – phonetagger Dec 18 '12 at 20:52
  • Please read the answer to [Getting cpu cycles using RDTSC - why does the value of RDTSC always increase?](http://stackoverflow.com/questions/8602336/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. – Jesse Good Dec 18 '12 at 20:52
  • Jasse's Good's comment solved the case, thanks for suggestions. – Ben Dec 18 '12 at 21:19

1 Answers1

1

Have you looked at the call clock?

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

abelenky
  • 63,815
  • 23
  • 109
  • 159
  • Fix me if i'm wrong. Basicly why i whant cpu cycles because it's constant, and clock readings depends on situation. – Ben Dec 18 '12 at 21:00
  • From the documentation: "Returns the processor time **consumed by the program.**" *(ie: not CPU-wide, just specific to that one program)* – abelenky Dec 18 '12 at 21:43
  • 1
    nice, this changes everything. – Ben Dec 18 '12 at 21:50