The
time
command is an excellent tool for analyzing the performance of a shell script or command. Simply type time
followed by the command that you wish to time. Three results are printed when the program or script finishes executing: the actual length of time (real-world time spent on the program), the total time spent in the program, and the total time spent on CPU overhead. The first figure is perhaps the most useful, but the third figure will tell you how busy your CPU is. Let us create a small shell script. Type in the following lines to a text file named myscript.sh
#!/bin/bash echo "Welcome to Unixlab "
Save the file and make it executable.
$ chmod u+x myscript.sh
Now run it using time
$ time myscript.sh
real 0m0.008s user 0m0.004s sys 0m0.000s My machine is too fast to show any perceptible time.
Now try to expand the myscript.sh by adding a for loop as below
#!/bin/bash for i in 1 2 3 4 5 do echo "Welcome to Unixlab " done
Run it
$ time myscript.sh
Welcome to Unixlab Welcome to Unixlab Welcome to Unixlab Welcome to Unixlab Welcome to Unixlab real 0m0.008s user 0m0.004s sys 0m0.004s
Still the real time is shown as 0.00. Let us build a loop that takes a long
time to finish and see the difference.
#!/bin/bash x=1 while [ $x -le 100 ] do echo "Welcome to Unixlab" x=$(( $x + 1 )) done
Again run it. I am getting the following timings.
real 0m0.016s
user 0m0.012s
sys 0m0.004s
Please read the time man page regarding accuracy of results and other
useful options.
No comments:
Post a Comment