October 31, 2025, 02:44:17 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


How measure time for FOR/NEXT loop

Started by aurelCB, July 08, 2010, 01:00:04 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

aurelCB

Hi all...
Is there a way to use timer to mesure how much time is pass from start to end of
FOR/NEXT loop?

LarryMc

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
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

aurelCB

Wow,this looks very simple!
Thanks Larry i will try..

Aurel

aurelCB

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 ???

LarryMc

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
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

LarryMc

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
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

sapero

July 08, 2010, 04:12:24 PM #6 Last Edit: July 08, 2010, 04:27:12 PM by sapero
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


LarryMc

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

aurelCB

Thanks Sapero i will try this option.