Timing Jobs with time
Sometimes, you might want to know how long a job takes to complete. You can do so using the time command, which times jobs according to the built-in Unix timer. As Code Listing 9.10 shows, all you have to do is enter time followed by the command you want to time.
Code Listing 9.10. Enter time plus the full job command to find out the job time.
[ejr@hobbes running]$ time slowscript 0.05user 0.06system 0:50.12elapsed 0%CPU [ejr@hobbes running]$ time ls bigdog.tar.gz slowscript testing.gif 0.03user 0.00system 0:00.03elapsed 78%CPU [ejr@hobbes running]$ time nice -19 ls bigdog.tar.gz slowscript testing.gif 0.03user 0.03system 0:00.06elapsed 93%CPU [ejr@hobbes running]$ time -p nice -n 19 ls foo real 0.00 user 0.00 sys 0.00 [ejr@hobbes running]$
To Time a Job Using time:
At the shell prompt, type time followed by the complete command. After the command finishes, the system will tell you how long it took, as shown in Code Listing 9.10.
To Compare Job Times with time:
time ls /usr
At the shell prompt, type time followed by a job (here, ls).
time nice -n 19 ls /usr
Then, type time followed by another job. In this example, we're comparing a regular ls command to a nice ls command. As Code Listing 9.10 shows, the elapsed time for the nice ls command was considerably longer than the regular ls command.
Keep in mind that the time a job takes to run may vary according to the system's current load or capacity. For example, a job might take less time to run at 2a.m., when few people are using the Unix system, compared with 2 p.m., when many more people are using the system.
Different systems produce slightly different time outputs. On some systems, you'll get real (clock) time, user time, and system time. Real time is how many seconds on the clock elapsed while the program was running, while user and system time both refer to different measures of how long it took the system to run the job. On other systems, you might get a ton of garbage, as shown in Code Listing 9.11, but the gist of the information is the same.
In the case of the ls example, you're really not concerned with either the output or the errorsjust the timeso you can creatively dispense with all of it. Try time ls /usr > /dev/null 2>&1 to send standard output to /dev/null (the bitbucket) and send error messages to standard output (and thence to the bitbucket). See Chapter 17 for details.
Try time p to get a more human-readable output.
Code Listing 9.11. time output varies from system to system. Here, we get a bunch of garbage to decipher in addition to the time information.
$ time slowscript 0.07user 0.05system 0:50.13elapsed 0%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (219major+59minor)pagefaults 0swaps $