Click here to Skip to main content
13,005,991 members (34,777 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Do you think finding time consumption of a process by a code below is right?
#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?

Posted 17-Feb-13 18:32pm
Updated 17-Feb-13 22:12pm
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?
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!
Rate this: bad
Please Sign up or sign in to vote.

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You may use GetTickCount also for this.


DWORD dwStartTime = ::GetTickCount();

/// Done the process

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

It will give you the time difference in milliseconds.

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.
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170628.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100