Bartosz Kowalczyk
Bartosz Kowalczyk

Reputation: 1519

Way to measure time of execution program

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?

Upvotes: 0

Views: 401

Answers (3)

In addition to other answers, mostly suggesting to use the time utility or shell builtins:

  • time(7) is a very useful page to read.
  • You might use (inside your code) the clock(3) standard function to get CPU time in microseconds.
  • Resolution and accuracy of time measures depends upon hardware and operating system kernel. You could prefer a "real-time" kernel (e.g. a linux-image-3.2.0-rt package), or at least a kernel configured with CONFIG_HZ_1000) to get more precise time measures.
  • You might also use (inside your code) the clock_gettime(2) syscall (so link also the -lrt library).
  • When doing measurements, try to have your measured process run a few seconds at least, and measure it several times (because e.g. of disk cache issues).

Upvotes: 3

Richard
Richard

Reputation: 61459

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 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

Upvotes: 2

brunobeltran0
brunobeltran0

Reputation: 74

If your program is named foo, then simply typing

~$ time foo

should do exactly what you want.

Upvotes: 4

Related Questions