Calculating time taken by your program to run in linux

This is a simple tips for some one who does not have UNIX background. It is easy to calculate time taken by a command, script or application to finish running in shell. The command is called time. It displays the clock time, CPU time spent on kernel mode and user mode by default.

The syntax is,

$ time <your command with arguments>

The following example shows time taken by du -sh command (to calculate the disk space occupied by current directory).

$ time du -sh .
60G .
real 0m5.297s
user 0m0.070s
sys 0m0.460s

It has taken 5.297 seconds to complete. CPU has spent 0.07 seconds on user mode and 0.46 seconds on kernel mode. This command is handy while trying to improve the performance of the applications.

For more about time command, try man time in your terminal.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *