views:

3221

answers:

3

I want to execute something in a linux shell under a few different conditions, and be able to output the execution time of each execution.

I know I could write a perl or python script that would do this, but is there a way I can do it in the shell? (which happens to be bash)

+20  A: 

Use the built-in time command:

$ help time

time: time [-p] PIPELINE
    Execute PIPELINE and print a summary of the real time, user CPU time,
    and system CPU time spent executing PIPELINE when it terminates.
    The return status is the return status of PIPELINE.  The `-p' option
    prints the timing summary in a slightly different format.  This uses
    the value of the TIMEFORMAT variable as the output format.
times: times
    Print the accumulated user and system times for processes run from
    the shell.

Example:

$ time sleep 2
real    0m2.009s
user    0m0.000s
sys     0m0.004s
Robert Gamble
thanks, I feel like i should have known this already
Rob
+7  A: 

You can get much more detailed information than the bash built-in time (which Robert Gamble mentions) using time(1). Normally /usr/bin/time

Example of verbose output:


$ /usr/bin/time -v sleep 1
       Command being timed: "sleep 1"
       User time (seconds): 0.00
       System time (seconds): 0.00
       Percent of CPU this job got: 1%
       Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.05
       Average shared text size (kbytes): 0
       Average unshared data size (kbytes): 0
       Average stack size (kbytes): 0
       Average total size (kbytes): 0
       Maximum resident set size (kbytes): 0
       Average resident set size (kbytes): 0
       Major (requiring I/O) page faults: 0
       Minor (reclaiming a frame) page faults: 210
       Voluntary context switches: 2
       Involuntary context switches: 1
       Swaps: 0
       File system inputs: 0
       File system outputs: 0
       Socket messages sent: 0
       Socket messages received: 0
       Signals delivered: 0
       Page size (bytes): 4096
       Exit status: 0

grepsedawk
The time command you referenced is not the bash built-in time command.
Robert Gamble
you wouldn't happen to know what debian package that would come from? doesn't seem to be installed by default
ʞɔıu
I was referencing your post Robert. Unless you mean the command you suggest is not the bash built-in?
grepsedawk
Amazingly enough, it's installed from a package called "time".
Paul Tomblin
The output from the /usr/bin/time looks like "0.00user 0.00system 0:02.00elapsed 0%CPU (0avgtext+0avgdata 0maxresident)k0inputs+0outputs (0major+172minor)pagefaults 0swaps"
Paul Tomblin
@grepsedawk: The time command I used was the bash built-in command, the time(1) command which you provided the man page for is not the built-in command but a separate standalone program, I thought you were confusing the two.
Robert Gamble
@Nick: "sudo apt-get install time".
Robert Gamble
Anyway, +1 because I never used the /usr/bin/time because of the ugly output but now I know how to get nicely formatted output and it provides a lot of other useful information (which I wouldn't expect from a command called "time").
Robert Gamble
+1  A: 

If you intend to use the times later to compute with, learn how to use the -f option of /usr/bin/time to output code that saves times. Here's some code I used recently to get and sort the execution times of a whole classful of students' programs:

fmt="run { date = '$(date)', user = '$who', test = '$test', host = '$(hostname)', times = { user = %U, system = %S, elapsed = %e } }"
/usr/bin/time -f "$fmt" -o $timefile command args...

I later concatenated all the $timefile files and pipe the output into a Lua interpreter. You can do the same with Python or bash or whatever your favorite syntax is. I love this technique.

Norman Ramsey