Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Do you think finding time consumption of a process by a code below is right?
C++
#include <time.h>
clock_t start, end;
double time;
start = clock();
/*process here*/
end = clock();
time = (double) (end - start)/CLOCKS_PER_SECOND;


I used it and I am getting time consumption of a process, but I need to verify whether it is right or wrong? and if there is any other solution, plz suggest me!!

by above code, if we find time, will it be in sec or msec?

thanx!!
Posted
Updated 17-Feb-13 22:12pm
v2
Comments
Sergey Alexandrovich Kryukov 18-Feb-13 0:38am    
More exactly, this is the elapsed time. Are you sure you need exactly this? The time will vary depending on other processes executed in the system. Is it fine by you?
—SA
jakr13 18-Feb-13 2:05am    
yes I have 5 process in my program and I have to calculate how much time consumed by each process . I have an another doubt, the elapsed time calculated here, is it in sec or millisec?
Stefan_Lang 18-Feb-13 6:18am    
You're just measuring the time that passes, not the time that an individual process consumes on the CPU. See solution 1!

The code calculates the time since starting your process in seconds.

If you need the execution times for Windows threads, you can use the GetThreadTimes()[^] function.
 
Share this answer
 
You may use GetTickCount also for this.

eg.

/// EXAMPLE CODE ///
DWORD dwStartTime = ::GetTickCount();

/// Done the process

DWORD dwDiff = ::GetTickCount() - dwStartTime;
/// /////////// ///

It will give you the time difference in milliseconds.

:)
 
Share this answer
 
Comments
jakr13 19-Feb-13 0:46am    
Which header file I should use fro GetTickCount(), because I never used it.
Vaibhav Meena 19-Feb-13 2:16am    
Its simple to use and you need to include "Winbase.h". And you may also visit this link once so you will have better understanding on this.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724408(v=vs.85).aspx
:)
Stefan_Lang 19-Feb-13 3:45am    
This only measures time, not CPU use.
Vaibhav Meena 19-Feb-13 4:23am    
Yes Stefan, you are right. I am also telling the same.
Both clock() and GetTickCount() use the system timer which only gets updated once every 10-16ms, depending on your hardware. For measuring time consumption of a process this time resolution is usually not sufficient.

More importantly, both methods do are not specific to a process - they just measure the global time, not the time a process uses on the CPU!

You should take a look at QueryPerformanceCounter[]. If you follow that link, you'll find a comment with a listing for a high resolution timer (class HRTimer) that refers only to the active time of the current thread.
 
Share this answer
 
Windows OS is preemptive operating system ( not RTOS ) and it allocates time for all the process in the system in a round robin fashion ( known as context switching ). So the calculation what you have done is not a exact one rather approximated.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900