Since you want to analyse the performance of a particular program, the right tool to do this would be perf
, which is included in linux-tools
package. It is a complex and powerful tool which ought to be learned before you can use it in full. However, obtaining CPU load statistics is easy:
$ perf stat ls /usr/bin
Performance counter stats for 'ls /usr/bin':
149.787143 task-clock # 0.535 CPUs utilized
3,648 context-switches # 0.024 M/sec
1 CPU-migrations # 0.000 M/sec
266 page-faults # 0.002 M/sec
78,313,233 cycles # 0.523 GHz
47,984,906 instructions # 0.61 insns per cycle
9,067,524 branches # 60.536 M/sec
1,113,291 branch-misses # 12.28% of all branches
0.279785985 seconds time elapsed
As you can see, ls
was executed in 0.28 seconds, consuming 0.535% of my two-core CPU (so, essentially loading 1 core at 100%). An average of 0.61 instructions per cycle suggests it's not heavily optimized, and so does the 12% of branch misses.
/proc/stat
(described inman proc
) which is the horse's mouth from with tools liketop
take measurements, I believe. But if it is not very performance critical then thenpopen()
on such a tool is easier.