• 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.
0
markserv
Asked:
markserv
3 Solutions
 
pchela733Commented:
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.
0
 
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?
0
 
Let_Me_BeCommented:
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?
0
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.

 
Infinity08Commented:
>> 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.
0
 
BrianGEFF719Commented:
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?
0
 
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.
0
 
markservAuthor Commented:
Question was answered in an appropriate manner.
0
 
Infinity08Commented:
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.
0
 
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?
0
 
Infinity08Commented:
These two help topics describe what the grades mean, and how to change one if needed resp. :

        http://www.experts-exchange.com/help.jsp#hi=403
        http://www.experts-exchange.com/help.jsp#hi=404
0

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