100 Useful Command-Line Utilities

by Oliver; 2014

37. time

As we mentioned above, time tells you how long your script or command took to run. This is useful for benchmarking the efficency of your program, among other things. For example, to time sleep:
$ time sleep 10
real	0m10.003s
user	0m0.000s
sys	0m0.000s
How do you tell how much memory a program or command consumed? Contrary to what its name suggests, the time command also gives us this information with the -v flag:
$ time -v sleep 10
-bash: -v: command not found
What?! We got an error. Why?
$ type time
time is a shell keyword
Confusingly, the reason is that time is actually a keyword, so we can't invoke it with flags as usual. To use it, we have to call the program by its full path:
$ /usr/bin/time -v sleep 10
	Command being timed: "sleep 10"
	User time (seconds): 0.00
	System time (seconds): 0.00
	Percent of CPU this job got: 0%
	Elapsed (wall clock) time (h:mm:ss or m:ss): 0:10.00
	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): 2560
	Average resident set size (kbytes): 0
	Major (requiring I/O) page faults: 0
	Minor (reclaiming a frame) page faults: 202
	Voluntary context switches: 2
	Involuntary context switches: 0
	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
Now we get the information payload.

<PREV   NEXT>