LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 06-20-2008, 12:28 PM   #1
unihiekka
Member
 
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.

Thanks!
 
Old 06-20-2008, 01:21 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
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 valgrind.org. 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.
 
  


Reply


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 03: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 05:49 PM
diff. between elapsed time and cpu time madhugp Linux - General 1 01-15-2007 07:57 AM
resetting CPU time? servnov Linux - Newbie 1 11-19-2005 09:26 PM
Xfree86 and Cpu Time ampex189 Linux - Software 0 02-07-2005 04:51 PM


All times are GMT -5. The time now is 04:30 PM.

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