Skip to content

Way to measure time of execution program

An answer to this question on Stack Overflow.

Question

I have a lots of short programs in C. Each program realize simple operation for example: include library, load something (ex matrix) from file, do simple operation, write matrix to file end.

I want to measure real time of excecution a whole program (not only fragment of code).

My simple idea is using htop or ps aux -> column time. But this method isn't good because I don't have exacly time of execution but time of excecution during last refresh and I can miss this.

Do you have any method to measure time of process in linux?

Answer

If you use

time <PROGRAM> [ARGS]

this will provide some base-level information. This should run your shell's time command. Example output:

$ time sleep 2
real	0m2.002s
user	0m0.000s
sys 	0m0.000s

But there is also

/usr/bin/time <PROGRAM> [ARGS]

which is more flexible and provides considerably more diagnostic information regarding timing. This runs a GNU timing program. [This site][1] has some usage examples. Example output:

$ /usr/bin/time -v sleep 2
Command being timed: "sleep 2"
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:02.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): 2496
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: 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

[1]: http://www.thegeekstuff.com/2012/01/time-command-examples/