IonicWind Software

IWBasic => General Questions => Topic started by: aurelCB on July 08, 2010, 01:00:04 PM

Title: How measure time for FOR/NEXT loop
Post by: aurelCB on July 08, 2010, 01:00:04 PM
Hi all...
Is there a way to use timer to mesure how much time is pass from start to end of
FOR/NEXT loop?
Title: Re: How measure time for FOR/NEXT loop
Post by: LarryMc on July 08, 2010, 01:40:03 PM
Based on something Sapero had done for me I would try something like this:

DWORD starttime = GetTickCount()
for
.....
next
DWORD stoptime = GetTickCount()
print stoptime-starttime


LarryMc
Title: Re: How measure time for FOR/NEXT loop
Post by: aurelCB on July 08, 2010, 02:33:48 PM
Wow,this looks very simple!
Thanks Larry i will try..

Aurel
Title: Re: How measure time for FOR/NEXT loop
Post by: aurelCB on July 08, 2010, 02:57:19 PM
Larry i dont find type DWORD then i use INT becose GetTickCount return INT ithink that is right?
here is code which return me 16- i think miliseconds,right?

'Larry starttime/stoptime
int starttime,stoptime
DECLARE IMPORT, GetTickCount(),INT
setprecision 6
OPENCONSOLE
starttime = GetTickCount()
FOR n = 1 TO 1000000
'
NEXT n
stoptime = GetTickCount()
PRINT stoptime-starttime

DO
UNTIL INKEY$ <> ""
CLOSECONSOLE
END


but i recive little bit strange results,sometimes is 16,sometimes 31 ???
Title: Re: How measure time for FOR/NEXT loop
Post by: LarryMc on July 08, 2010, 03:02:22 PM
Yes the return is in milisecs

I used DWORD because that was what Sapero gave me.

The reason it works for me is that I always use

$include "windowssdk.inc" in all my programs which is Sapero's inclue files.

I'll run your little program on my computer with int and with dword and see if I get any difference.

LarryMc
Title: Re: How measure time for FOR/NEXT loop
Post by: LarryMc on July 08, 2010, 03:10:45 PM
Aurel
I put your loop inside of another loop of 10
With INT I get 7-8 out of 10 as 15-16 and the rest 0

With DWORD I get 3-4 of 1o as 0 and the rest are 15,16,17 or 31.

It might be that with the time sharing going on inside the computer it is varying our results.

Sapero or one of the other guys will tell us what's going on.

Lightening here.  shutting my computer down.

LarryMc
Title: Re: How measure time for FOR/NEXT loop
Post by: sapero on July 08, 2010, 04:12:24 PM
GetTickCount has a very poor resolution, ~16ms, use multimedia timer timeGetTime (the same prototype, better resolution 1ms), or better QueryPerformanceCounter, with a resolution of ~280ns.

Call QueryPerformanceCounter at the beginning and end of your loop. Each query will return an 64-bit counter value (uint64). Divide the difference by timer frequency (QueryPerformanceFrequency)

uint64 freq, time1, time2
QueryPerformanceFrequency(&freq) ' call once, frequency will never change

QueryPerformanceCounter(&time1)
' your loop is here
QueryPerformanceCounter(&time2)
double time = (time2 - time1) / (freq+0.0) ' in seconds


System time functions have even better resolution: 100ns - GetSystemTime or GetLocalTime. The time is returned to user allocated SYSTEMTIME structure, but SystemTimeToFileTime can convert it to 64-bit value. If you would like to try it out, just remove QueryPerformanceFrequency, set freq to a constant value 1/100ns = 10000000 (10MHz), and QueryPerformanceFrequency replace with GetSystemTime+SystemTimeToFileTime:
uint64 time1, time2
SYSTEMTIME st

GetSystemTime(&st)
SystemTimeToFileTime(&st, &time1)
' your loop is here Sleep(500)
GetSystemTime(&st)
SystemTimeToFileTime(&st, &time2)
double time = (time2-time1)/10000000.0

Title: Re: How measure time for FOR/NEXT loop
Post by: LarryMc on July 08, 2010, 05:04:21 PM
Neat!

Thanks Sapero!!

LarryMc
Title: Re: How measure time for FOR/NEXT loop
Post by: aurelCB on July 09, 2010, 01:58:01 AM
Thanks Sapero i will try this option.