• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1462
  • Last Modified:

CPU time taken to run a process.

Hey all.

I've written a C++ program that will take a given process name (eg: notepad.exe) and then when that process has finished, it will return the total CPU time (kernel time + user time) used by that process. this is done using GetProcessTimes() and a call to WaitForSingleObject() to wait for the process to finish.

this seems to work and returns expected results. the only problem is that if i run my program again on the same process it returns different results.

an example of this is i ran notepad, fired up my program to latch onto notepad.exe, wrote some text in notepad and closed it. my program might come back with 15ms cpu time spent.
if i do this same procedure again it comes up with varying results +-3ms.

on larger processes that took several minutes the results varied +-20seconds.

is this to do expected or am i doing something totally wrong? can paste code on request.
3 Solutions
This is totally normal, because every time the different processes executed on the processor and the process, you monitor, may get less access to CPU and take longer time to execute.
markservAuthor Commented:
which is what i thought happens.
BUT isn't the whole idea of using getprocesstimes() the fact that you get the AMOUNT of CPU time spend processing, not elapsed (real time).
i guess an example would help.

if I use my program to monitor notepad. it might come back with a total CPU time of 5seconds but an elapsed time of several minutes. this is because the process was running for 5minutes but only used 5seconds of the cpu.

if i duplicate the exact things i did in notepad the next time around, one would expect that total CPU time would be the same.....no?
Well, that depends. Is your code completely deterministic? Meaning, does your code follow the same path each time it is executed?
You could have some busy waiting somewhere (causing the load).

Btw. we are talking about lpUserTime, right?
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

>> i ran notepad, fired up my program to latch onto notepad.exe, wrote some text in notepad and closed it.

Did you attach your timer at the exact same moment (ie. was notepad.exe in the exact same state) ? Did you take the exact same amount of time to repeat your user interactions ? Did the time between the keystrokes match ? Did you close notepad at exactly the same moment (ie. was notepad.exe in the exact same state) ?

I'd say it's pretty much impossible to perform this test in such a reliable and precise way that you'd get the exact same timing results.

So, yes it's normal.

A way to get better results, is to perform the same test several times, and take the average of all timing results.
Unfortunately your testing method is not reliable. There are many unknown input latencies, especially related to user input. As Infinity08 already mentioned the only reliable way is to average results, and be more consistent with your tests.

Better yet, why do you want to do this anyway?
markservAuthor Commented:
Thanks for the replies guys.

I figured that the total CPU time for a given process would vary each run and that the only way to do it would be to run the process serveral times and get an average.
markservAuthor Commented:
Question was answered in an appropriate manner.
May I ask why you gave a B grade ? Usually that means that something was missing in the answer and/or that something is still unclear. If that's the case, then please don't hesitate to ask for clarification where needed.
markservAuthor Commented:
Oh sorry. I thought grading was kind of like "Giving a mark to a student!" in which case I usually don't like to give the top mark unless it was "above and beyond". Can i change it, since my question was fully answered?
These two help topics describe what the grades mean, and how to change one if needed resp. :


Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now