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-14-2006, 10:33 PM   #1
Registered: Oct 2003
Posts: 354

Rep: Reputation: 30
calcualte the execution time of a function

Hello everyone,

What is the convenient way to calculate the execution time of a function? The function may be not a long and time consuming function -- only 10 lines in some situation.

thanks in advance,
Old 06-14-2006, 11:55 PM   #2
Senior Member
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 59
does the 'time' command help?
Old 06-15-2006, 01:54 AM   #3
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
There are 3 times to consider:
1- The system times: the time spent by the kernel on behalf the process, that is: servicing system calls, doing thread related computations too in systems with native threads, page faults and so on.

2- The CPU times: the effective time spent by the process in the CPU. This is independent from system load.

3- The clock time: the total time spent from start to finish (what the user would notice anyway). It isn't precise as it depends on system load.

To get them you may use the getrusage() system call for #1 & #2 and gettimeofday() for #3, or times() to get them all. I prefer getrusage()/gettimeofday() because it has microsecond resolution.

PS: Depending on what you are trying to measure, you may use the sum of #1 and #2. Use #2 when you want to measure computations and #1 to measure system call overhead.

PS: Also, you may get rusage information for each child using wait3()/wait4()

Last edited by primo; 06-15-2006 at 01:59 AM.
Old 06-15-2006, 02:36 AM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,198

Rep: Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310
time is useful cmd, but you can get more info from the GNU version '\time':

[ ~]$ time ./

real 0m0.210s
user 0m0.116s
sys 0m0.016s
[ ~]$ \time ./
0.11user 0.00system 0:00.13elapsed 95%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+1357minor)pagefaults 0swaps
Old 06-15-2006, 03:13 AM   #5
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
If you more precise time information (time spent in each function), have a look at "gprof".


execution, function, time

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
command execution time minil Programming 10 08-20-2010 04:59 PM
retrieving execution time for process dipenchaudhary Programming 3 02-24-2006 10:27 AM
recursive execution of function in subdirectories Melsync Programming 3 01-17-2006 06:13 AM
how to [time] the execution hq4ever Linux - General 8 07-03-2004 04:57 AM
Timed execution of a function arvind_tyche Programming 1 05-07-2004 08:25 AM

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