A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/linux-unix/how-to-find-time-taken-by-a-program-on-linux-shell/ below:

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

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

Last Updated : 23 Jul, 2025

Linux has a time command. It's a command-line utility that reports precisely how long a command executes, in real time (wall-clock), user time (CPU time spent in user mode), and system time (CPU time spent in kernel mode). With one easy command, you can easily gauge performance, identify slowdowns, and tune your scripts or programs.

How the time Command Works 

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.

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.

CPP
#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.
Advanced things Using time Command in Linux

If you are a developer, system administrator, or student, mastering the Linux time command will help you in analyzing performance and debugging slow programs.

1. Compare Shell Built-in vs GNU time
/usr/bin/time -v ./fib  # -v = verbose mode for detailed stats
2. Save Output to a File for Later Review

Redirect the time output of the command to a file for debugging, reporting, or monitoring over time:

(time ls -l) 2> time_results.txt
3. Format the Output for Custom Reports

Utilize GNU time to format the way performance data is displayed. This is useful for dashboards, monitoring scripts, or developer reports.

/usr/bin/time -f "Real: %e sec | CPU: %U sec" ./fib

You can format output to include:

When to Use Real, User, or Sys Time

Understanding this when we use this:

Conclusion

The Linux time command isn't just a stopwatch — it's an effective performance debugging tool. Whether you're compiling source, executing scripts, or playing with cloud tasks, being able to know how long a command will execute helps you to create more efficient scripts, locate bottlenecks, and make smarter resource choices.

From system time to user time comparison to saving the output for later review, time provides an advantage in debugging, troubleshooting, and optimization. The next time your terminal is slow or your code is too slow, don't wonder — use time



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4