Calculating the execution time for a command

While testing an application's efficiency or comparing different algorithms to solve a given problem, the execution time taken is very critical. A good algorithm should execute in a minimum amount of time. Let's see how to calculate the execution time.

How to do it...

  1. To measure the execution time, just prefix time to the command you want to run.

    For example:

    $ time COMMAND
    

    The command will execute and its output will be shown. Along with the output, the time command appends the time taken in stderr. An example is as follows:

    $ time ls
    test.txt
    next.txt
    real    0m0.008s
    user    0m0.001s
    sys     0m0.003s
    

    It will show real, user, and system times for execution.

    Note

    An executable binary of the time command is available ...

Get Linux Shell Scripting Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.