How to measure CPU cycles per instruction in program C

I have a C program where I start using some SIMD optimizations for SPE (Cell processor), etc. I would like to somehow "time" how many cycles they need. One idea is to turn on / off and measure the entire runtime. But it is slow. I can also add between and before executing gettimeofday (& start, NULL) and so that the statements, but they are accurate, I think when you deal with more than milliseconds.

I wonder if it is possible to effectively measure nanoseconds per instruction, or just processor cycles or some other exact time measure.

+4
source share
4 answers

Depending on your processor, you can get performance indicators in the processor itself that track team hours and many other useful things. Profiles and other performance utilities can do this, so this should also be possible from user code. On Mac OS X, I would use the Apple CHUD framework, but you did not specify which OS or processor you are using, so it’s hard to give specific suggestions.

+4
source

Perform code verification in a loop and divide the time it takes for the loop counter. The timer you use should not have high resolution to measure the correct values.

+1
source

Nano seconds is not enough for this. You need picoseconds.

I do not think you can accurately measure something like that. You will need to study the specifications (I'm not sure if current processors have this information in the docs).

0
source

If not C guy ... I think you need to look at the build code and go from there. The only problem is that one command can take 1 or 100,000 processor cycles, depending on which processor you are on.

0
source

All Articles