PDA

View Full Version : Different loops type. Speed test script



ErosOlmi
18-03-2007, 10:58
How fast are thinBasic loops? Here the answer.


'-----------------------------------------------------------------------------
'Variable declaration
'-----------------------------------------------------------------------------
Dim T0, T1, T2, T3, T4, T9 AS quad

DIM MaxLoops AS LONG VALUE 100000
DIM Count AS LONG
DIM tmpNumber AS QUAD
DIM tmpString AS STRING
DIM Message AS STRING
DIM tFormat AS STRING VALUE "#0"

'-----------------------------------------------------------------------------
'Confirm script execution
'-----------------------------------------------------------------------------
Message = "This program will perform:\n"
Message += "" & MaxLoops & " FOR\n"
Message += "" & MaxLoops & " WHILE/WEND\n"
Message += "" & MaxLoops & " DO/LOOP WHILE\n"
Message += "" & MaxLoops & " DO/LOOP UNTIL\n"
Message += "Please press Yes to go on, NO to Stop\n"

DIM lResult AS LONG = MSGBOX(0, Message, %MB_YESNO, "Continue?")
IF lResult <> %IDYES THEN
STOP
END IF

T0 = GetTickCount

'-----------------------------------------------------------------------------
'Test speed: FOR loop
'-----------------------------------------------------------------------------
T1 = GetTickCount
tmpNumber = 2
FOR Count = 1 TO MaxLoops
tmpNumber += 1
tmpNumber = (tmpNumber * 2) ^ 2
tmpNumber = 2
NEXT

'-----------------------------------------------------------------------------
'Test speed: WHILE loop
'-----------------------------------------------------------------------------
T2 = GetTickCount
Count = 0
tmpString = ""
tmpNumber = 2
WHILE Count < MaxLoops
Count += 1
tmpNumber = (tmpNumber * 2) ^ 2
tmpNumber = 2
WEND

'-----------------------------------------------------------------------------
'Test speed: DO loop WHILE
'-----------------------------------------------------------------------------
T3 = GetTickCount
Count = 0
tmpString = ""
tmpNumber = 2
DO
Count += 1
tmpNumber = (tmpNumber * 2) ^ 2
tmpNumber = 2
LOOP WHILE Count < MaxLoops

'-----------------------------------------------------------------------------
'Test speed: DO loop UNTIL
'-----------------------------------------------------------------------------
T4 = GetTickCount
Count = 0
tmpString = ""
tmpNumber = 2
DO
Count += 1
tmpNumber = (tmpNumber * 2) ^ 2
tmpNumber = 2
LOOP UNTIL Count >= MaxLoops

T9 = GetTickCount

'-----------------------------------------------------------------------------
'Show results with some internal counters
'-----------------------------------------------------------------------------
Message = "Test " & MaxLoops & " loops." & $CRLF
Message += MaxLoops & " FOR" & $tab & FORMAT$(T2 - T1, tFormat) & " msec." & $CRLF
Message += MaxLoops & " WHILE" & $tab & FORMAT$(T3 - T2, tFormat) & " msec." & $CRLF
Message += MaxLoops & " DO WHILE" & $tab & FORMAT$(T4 - T3, tFormat) & " msec." & $CRLF
Message += MaxLoops & " DO UNTIL" & $tab & FORMAT$(T9 - T4, tFormat) & " msec." & $CRLF
Message += "Total " & $tab & FORMAT$(T9 - T0, tFormat) & " msec." & $CRLF
Message += "Final message" & $tab & FORMAT$(GetTickCount - T9, tFormat) & " msec." & $CRLF

MSGBOX(0, Message)

kryton9
18-03-2007, 23:00
Clever test Eros. I was surprised by how much faster the do while loops were compared to the good old for loop.

ErosOlmi
18-03-2007, 23:09
Ken,

all those types of loop are managed internally by thinBasic core in different way.
What is best mostly depend by the processor and how thinBasic techniques best fits processor characteristics.
For example in my tests FOR/NEXT is always the best. If in your tests on AMD it is not it means the technique used is not the best on AMD processors.
Every processor manufacturer has different tips and tricks to get the max from theyr toys I suppose

In any case I think we are talking about few milliseconds difference ;)

Ciao
Eros

Dolphkex
08-02-2024, 16:43
Have you observed significant variations in loop execution speed across different types, and do you think these differences are influenced by processor characteristics?