How to find time taken by a command/program on Linux Shell?

We have already discussed a way to find time taken by a function through C libraries. If we are on Linux, then it becomes very easy to find time taken by a program/command.

We can use time command for this purpose. The time taken is shown in three forms.
real: Total end to end time taken by program/command
user: Time taken in user mode.
sys: Time taken in kernel mode


A Command Example (Time taken by ls-l):



$ time ls -l

The above command runs "ls -l" and shows 
contents of current directory followed by
the time taken by command "ls -l".  

 

A program example (Time taken by fib(30)):
let us consider below program.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int fib(int n)
{
   if (n <= 1)
      return n;
   return fib(n-1) + fib(n-2);
}
  
int main ()
{
  printf("Fibonacci Number is %d", fib(30));
  return 0;
}

chevron_right


Let we save above program as fib.c.

// Compiling above program on shell
~$ gcc fib.c

// Running the generated executable with time
~$ time ./a.out
Fibonacci Number is 832040
real    0m0.017s
user    0m0.017s
sys    0m0.000s

Note: 0.017 seconds (shown with real) is total 
time taken by program.

This article is contributed Dheeraj Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.