Board index » Visual Studio » Measuring time

Measuring time

Visual Studio57
Hello,



I am using the following lines of code to measure the time taken by a

function in a MFC application(on Windows 2000/Windows XP) and to

display the time in milliseconds on the main dialog.



DWORD startTime=0;

DWORD ElapsedTime=0;

DWORD endTime=0;

CString dispTime="";



startTime = GetTickCount();



//function is called here

...........................



endTime=GetTickCount();

ElapsedTime = endTime - startTime;

dispTime.Format("%d ms",ElapsedTime);

m_sBerTime = dispTime;

UpdateData(FALSE);



The problem is that the times measured and displayed vary as much as

by more than 100 ms some times. Is this a normal behavior when using

GetTickCount() or am I doing something wrong here? Is there some

better way to do this to get accurate time in ms?



Thanks in advance for any reply.


-
 

Re:Measuring time

Quote
The problem is that the times measured and displayed vary as much as

by more than 100 ms some times. Is this a normal behavior when using

GetTickCount() or am I doing something wrong here? Is there some

better way to do this to get accurate time in ms?



You can use QueryPerformanceCounter, QueryPerformanceFrequency to get

more accurate timing than is possible with GetTickCount.



Dave

--

MVP VC++ FAQ: www.mvps.org/vcfaq">www.mvps.org/vcfaq

-

Re:Measuring time

seia0106 wrote:

Quote
Hello,



I am using the following lines of code to measure the time taken by a

function in a MFC application(on Windows 2000/Windows XP) and to

display the time in milliseconds on the main dialog.



DWORD startTime=0;

DWORD ElapsedTime=0;

DWORD endTime=0;

CString dispTime="";



startTime = GetTickCount();



//function is called here

...........................



endTime=GetTickCount();

ElapsedTime = endTime - startTime;

dispTime.Format("%d ms",ElapsedTime);

m_sBerTime = dispTime;

UpdateData(FALSE);



The problem is that the times measured and displayed vary as much as

by more than 100 ms some times. Is this a normal behavior when using

GetTickCount() or am I doing something wrong here? Is there some

better way to do this to get accurate time in ms?



Thanks in advance for any reply.



There are two large problems with this method. (1) Your program will

frequently be suspended by Windows as it performs other tasks and as it

executes other applications. Even if your function under test consumes

zero time, hundreds of milliseconds could elapse between the first and

second call to GetTickCount. (2) The timer that Windows returns with

GetTickCount only changes at 10-55 millisecond intervals, depending on

which Windows version you are using. You can use

QueryPerformanceCounter to get much better timing resolution.



--

Scott McPhillips [VC++ MVP]



-

Re:Measuring time

GetTickCount is just about the most useless way in existence to measure program

performance. It has nothing to do with the time spent in your program, it deals with clock

time, and therefore if anything else is going on, that gets counted also. With a

resolution in tens of milliseconds. So if you get preempted by some other threads of

control, they get counted also.



Use QueryPerformanceCounter/QueryPerformanceFrequency if you want reliable timing values.

You convert QueryPerformanceCounter values to ms by using the QueryPerformanceFrequency

value.



Also, try to avoid using UpdateData. See the essay on my MVP Tips site on why this is a

really bad way to manipulate controls.

joe



On 22 Aug 2004 01:17:00 -0700, miahmed67@yahoo.com (seia0106) wrote:



Quote
Hello,



I am using the following lines of code to measure the time taken by a

function in a MFC application(on Windows 2000/Windows XP) and to

display the time in milliseconds on the main dialog.



DWORD startTime=0;

DWORD ElapsedTime=0;

DWORD endTime=0;

CString dispTime="";



startTime = GetTickCount();



//function is called here

...........................



endTime=GetTickCount();

ElapsedTime = endTime - startTime;

dispTime.Format("%d ms",ElapsedTime);

m_sBerTime = dispTime;

UpdateData(FALSE);



The problem is that the times measured and displayed vary as much as

by more than 100 ms some times. Is this a normal behavior when using

GetTickCount() or am I doing something wrong here? Is there some

better way to do this to get accurate time in ms?



Thanks in advance for any reply.



Joseph M. Newcomer [MVP]

email: newcomer@flounder.com

Web: www.flounder.com">www.flounder.com

MVP Tips: www.flounder.com/mvp_tips.htm">www.flounder.com/mvp_tips.htm

-