use time command as a simple benchmark tool

October 20th, 2007 mysurface Posted in Calculation, time | Hits: 77952 | 10 Comments »

Sometimes you need to track execution time for some apps for benchmarking, time command is a handy tool. Let say you have wrote few scripts trying to grep some info from some log files, each of the scripts uses different techniques or command combos. You can run your scripts along with time command, and compare their’s execution time.

Let’s assume you have written two script name script1.sh and script2.sh. You can do this

time ./script1.sh

Same thing to script2.sh, and the results will looks as bellow:

real    0m1.005s
user    0m0.000s
sys     0m0.008s

real shows you the execution time script1.sh takes, o minutes 1.005 seconds. user and sys tracks the CPU processing time. From the results, shows us that script1.sh uses 8 milliseconds CPU processing time in kernel mode (sys) and None in user mode.

10 Responses to “use time command as a simple benchmark tool”

  1. Wow — that’s exactly what I needed. I wish I had a good, long script I could use it with, though. :(

  2. Thanks!

    So simple but so useful

  3. Excellent!!!

    Elegant as always, i love Linux :)

  4. Useful, thanks!

  5. I have a simple short script for test. (cursor)
    The only output I can get is:

    {$}[4] time ./cursor
    0.005u 0.062s 0:06.85 0.8% 0+0k 0+0io 0pf+0w

    If I try: time -f ./cursor or any other switch I get:
    -f: Command not found.

    even if I try time -V or –version it’s still: command not found… ???

  6. I had the same problem.

    try it with /usr/bin/time.

    I found the solution at:
    http://www.oreillynet.com/linux/cmd/cmd.csp?path=t/time

  7. hey, can someone tell me how to copy the result of the time function to a file? I intend to run a program inside the script 100000 times and i need to copy the output of the time function inside the script to a file..

  8. Thanks for the simple solution :)

    Ajay:
    time script.sh > output 2> err.log

    You can also drop 2> err.log to have errors on stderr(usually sent to stdout) instead

  9. I added the following alias to my .bashrc

    alias casio=’/usr/bin/time -f “\n%E elapsed,\n%U user,\n%S system,\n%M memory\n%x status”‘

    so now i just

    # casio ls -alhF /opt/somefolder

    and I get the time the process took to run

  10. I had to test my Perl script’s execution time, I could have used NYTProf but then someone on IRC suggested me to use “time perl script.pl”. Worked like a charm. Awesome utility!

Leave a Reply