Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-10-2006, 04:00 AM   #1
Registered: Jul 2004
Distribution: Suse 10.2
Posts: 43

Rep: Reputation: 15
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
x = x* y
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
Old 11-10-2006, 05:04 AM   #2
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Just the link you are looking for:
As long as you have an amd cpu.
Old 11-10-2006, 05:15 AM   #3
Senior Member
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (
Posts: 1,900

Rep: Reputation: 68
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).
Old 11-10-2006, 01:11 PM   #4
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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.
Old 11-10-2006, 04:43 PM   #5
Registered: Jul 2004
Distribution: Suse 10.2
Posts: 43

Original Poster
Rep: Reputation: 15
Awesome, thanks guys


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursively count lines of code in source dir heirarchy michaelsanford Programming 8 04-01-2006 04:59 PM
Shorewall & Smoothwall both - waste of cpu cycles? alaskazimm Linux - Security 1 09-29-2005 07:24 AM
How to count how many lines of code my project is? The_Nerd Programming 4 08-30-2004 08:26 PM
Code Count liguorir Programming 1 04-24-2004 01:56 PM
%CPU , Memory Usage, Thread count srinivasar Programming 0 09-20-2003 03:56 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:24 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration