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.
#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; } |
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
Recommended Posts:
- Introduction to Linux Shell and Shell Scripting
- A Shell program To Find The GCD | Linux
- Making your own Linux Shell in C
- Developing a Linux based shell
- Basic Shell Commands in Linux
- How to Install Z Shell(zsh) on Linux?
- Getting System and Process Information Using C Programming and Shell in Linux
- Kali Linux - Terminal and Shell
- RANDOM Shell Variable in Linux with Examples
- Difference Between Single and Double Quotes in Shell Script and Linux
- Bash shell script to find sum of digits
- Bash shell script to find out the largest value from given command line arguments
- Reverse a String | Shell Programming
- Array Basics Shell Scripting | Set 2 (Using Loops)
- Array Basics in Shell Scripting | Set 1
- How To Use Bash Shell Natively On Windows 10
- Bash shell script to swap two numbers
- Looping Statements | Shell Script
- Conditional Statements | Shell Script
- String Operators | Shell Script

