I'm looking for a way to measure CPU usage of a process that finishes in 1 second.
Since it's so fast, top
doesn't do it justice. From what I understand, top
takes snapshots, so the process could be done in between the updates.
The program is in C++, and I'm running on Linux. If there's some easy code that I can copy and paste into my program that prints out CPU usage at end of main()
, that'd work. Or, if there's some profiling tool, I could use that too.
EDIT - It seems like people have some misunderstanding of what I want.
I'm not looking for duration. I know the duration. It's around 1 second. What I want to know is CPU usage. If it's 100%, then it means my CPU was running the entire 1 second.
If it's 50%, then it means the CPU was idle 50% of the time. It's possible that it's waiting for IO the other 50%.
If my program ran for a long time, top
is good, since it shows stuff like this
%Cpu(s): 0.6 us, 0.3 sy, 0.0 ni, 99.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
This says my cpu is in user space 0.6% of the time, kernel space 0.3% of the time, and simply idle 99.1% of the time.
But - as I said before, top
doesn't work for fast processes. So what should I do?
thanks