LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I want to count CPU cycles used by arbitrary code (https://www.linuxquestions.org/questions/programming-9/i-want-to-count-cpu-cycles-used-by-arbitrary-code-500282/)

JonBrant 11-10-2006 04:00 AM

I want to count CPU cycles used by arbitrary code
 
I want a compiler, or an addon for a compiler, or whatever, that will let me find the number of clock cycles used by my computer to execute a block of code. Example - in C++ I might want to see just how many clock cycles are taken to multiply two given numbers numbers
Code:

#beginClockCycleCounter
x = x* y
#bendClockCycleCounter

On installation of it, it could generate a list of assembly commands, look at the block of code in assembly, and count them from there. The compiler basically converts it to assembly anyway (not really, but close enough to maybe work) or it could simply use the program counter and adjust for any jumps that execute.

I've never seen anything like this, but it would be useful as hell for something like deciding which algorithm is best to do something, or rating how fast a system should be to run it, etc

dmail 11-10-2006 05:04 AM

Just the link you are looking for: http://devforums.amd.com/calinux.jsp
As long as you have an amd cpu.

raskin 11-10-2006 05:15 AM

Well, your example is just useless - x*y has no defined sense of time of execution. Maybe you want just write function that will write result of rdtsc assembler command (is returned in edx:eax) to specified variable, and you will be able to measure difference. As I understand, precision is never better than 32 cycles, and if OS takes control away you get much more... But try increasing priority (OS- and permission- dependent).

matthewg42 11-10-2006 01:11 PM

What you need to use is gprof. You compile your program with the the -pg option, and when you run it, it creates a file called gmon.out. Then you run gprof on your executable, and it prints out of profiling graph with a breakdown of time spent in the various functions etc.

See here of more details.

JonBrant 11-10-2006 04:43 PM

Awesome, thanks guys


All times are GMT -5. The time now is 11:14 PM.