Help answer threads with 0 replies.
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 06-20-2008, 01:28 PM   #1
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Rep: Reputation: 32
CPU time of C++ programme

Is there a (standard) way of getting the CPU time used by a (part of a) C++ programme, such that I can print it on screen or append to a data file created by the programme itself? The clock function works nicely if I want the elapsed time, but I need the actual CPU time, if that's possible.

Old 06-20-2008, 02:21 PM   #2
jim mcnamara
Registered: May 2002
Posts: 964

Rep: Reputation: 36
getrusage() gives cpu time in kernel & user mode, but for the program as a whole and separately for the children as a whole.

For a particular function, there is no good run-time way to do this. However check at There are profiling tools there. But profiling adds a lot of overhead to your program.

This leaves two options -
use gprof the gnu profiler
"manually" keep track of cpu usage into and out of a particular function with getrusage and delta times.

Try gprof first. Compile with with gcc -g -p -o filename filename.c.
Running the code produces an output file, usually mon.out. gprof filename produces a lot of output. See if that doesn't do what you want before going to a lot of trouble. Re-compile without -p -g after you work out problems with performance.


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
kacpid eating cpu 99% cpu time! tjclancy Linux - Software 13 09-19-2011 04:08 PM
How can i make sure there is only one my programme is running at the same time hcj9999 Programming 6 06-19-2008 06:49 PM
diff. between elapsed time and cpu time madhugp Linux - General 1 01-15-2007 08:57 AM
resetting CPU time? servnov Linux - Newbie 1 11-19-2005 10:26 PM
Xfree86 and Cpu Time ampex189 Linux - Software 0 02-07-2005 05:51 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:33 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