Sunday, March 2, 2008

Understand your code's performance using time command - BASH

time - run programs and summarize system resource usage

The time command is useful to measure/understand your code's performance. The usual output consists of real, user and system time.
Real time is the amount of time between when the code started and when it exited.
User time and system time are the amount of time spent executing application code versus kernel code, respectively.

Two types of time command are available.

Shell's in-build time: Gives only scheduler information
/usr/bin/time: Gives more information, also allows formatting the output

To save the "time" details in a file:
$ /usr/bin/time -p ./ 2> time.out

Illustrating the use of "time" command with an example below:

I have two scripts for the same purpose(subdividing a file - sending each line to a new file named as the line-number, also adding a line called HEADER as first line of the

files, and a blank line as the last or 3rd line), but written in two different ways.
Now to compare the performance of the two scripts:

$ cat
awk '{print "HEADER" > "file_" NR} {print > "file_" NR} {printf("\n") > "file_" NR}' thefile.out

$ cat
cat thefile.out | awk '{ print "HEADER"
print "" }' | awk '{print >("file_" int((NR+2)/3))}'

$ time ./
real 0m0.130s
user 0m0.060s
sys 0m0.107s

$ time ./
real 0m0.178s
user 0m0.152s
sys 0m0.091s

So is a bit expensive performance wise compared to

No comments:

© Jadu Saikia