Link to home
Start Free TrialLog in
Avatar of markserv
markserv

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of pchela733
pchela733

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of markserv
markserv

ASKER

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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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.
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.
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. :

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