Wednesday, April 04, 2007

Use time command to get CPU usage too.

The linux time command is a nice little utility that can tell you how much time (cpu and real time) a command or program takes to finish. For example to find out how much time it takes in your computer to find certain file on you home directory you can use:

time find /home -name "filename"

real 0m16.365s
user 0m0.208s
sys 0m0.492s

The time command simply receives whatever program you pass it along with all the parameters you give it and gives you back some statistics about the program execution.

If you read the manual page you can see that time can tell you a lot more than just excecution time of program/commands.

In BASH the way to make time give you other values in other formats it to set up the "TIMEFORMAT" environment variable before calling time. For example to get the above time values plus the CPU percentage we use:

export TIMEFORMAT="%E real,%U user,%S sys, %P cpu"

time find /home -name "*.txt"

7.184 real,0.292 user,0.504 sys, 11.08 cpu

Note that the man page method (i.e. -f switch) does not work with the version of Bash that comes with Ubuntu Server and Kubuntu Edgy/Feisty.

No comments:

Post a Comment