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)
'-----------------------------------------------------------------------------
'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)