Results 1 to 4 of 4

Thread: Different loops type. Speed test script

  1. #1
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,818
    Rep Power
    10

    Different loops type. Speed test script

    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)
    
    Last edited by ErosOlmi; 08-02-2024 at 19:48.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  2. #2
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    68
    Posts
    3,865
    Rep Power
    405

    Re: Different loops type. Speed test script

    Clever test Eros. I was surprised by how much faster the do while loops were compared to the good old for loop.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,818
    Rep Power
    10

    Re: Different loops type. Speed test script

    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
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  4. #4
    Have you observed significant variations in loop execution speed across different types, and do you think these differences are influenced by processor characteristics?

Similar Threads

  1. NEO speed test result
    By kryton9 in forum thinBasic General
    Replies: 9
    Last Post: 02-05-2007, 20:44

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •