Results 1 to 17 of 17

Thread: [RESOLVED] Visual Basic - Code Syntax Speed

  1. #1

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Resolved [RESOLVED] Visual Basic - Code Syntax Speed

    Hello
    i am still new to visual basic i coded in RealBasic for years i wrote a code i was trying to convert to from RealBasic to VisualBasic its a speed benchmark test the speed of a piece code used i used to use this code below in RB to profile my code any ideas how i can it can be converted to VisualBasic? Thanks


    Code:
    Private Sub Command1_Click()
    Dim i As Integer
    Dim max As Integer
    max = 2000000
    Dim dump As Integer
    Dim x As Integer
    
    Dim start1, start2, start3 As Double
    Dim end1, end2, end3 As Double
    
    start1 = Microseconds
    For i = 0 To max
      x = x Mod 3
      If x = 0 Then dump = 0
      If x = 1 Then dump = 1
      If x = 2 Then dump = 2
    Next
    end1 = Microseconds
    
    start2 = Microseconds
    For i = 0 To max
      x = x Mod 3
      If x = 0 Then
        dump = 0
      ElseIf x = 1 Then
        dump = 1
      ElseIf x = 2 Then
        dump = 2
      End If
    Next i
    end2 = Microseconds
    
    start3 = Microseconds
    For i = 0 To max
      x = x Mod 3
      Select Case x
      Case 0
        dump = 0
      Case 1
        dump = 1
      Case 2
        dump = 2
      End Select
    Next
    end3 = Microseconds
    
    MsgBox = Format(end1 - start1, "0") + EndOfLine + _
      Format(end2 - start2, "0") + EndOfLine + _
      Format(end3 - start3, "0")
    End Sub

  2. #2
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: Visual Basic - Code Syntax Speed

    Yea... or you could use GetTickCount API.
    "Retrieves the number of milliseconds that have elapsed since the system was started"
    So retrieve it once before you execute your code, do your code, and retrieve it again, now deduct from one another.

    Code:
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    Code:
    Dim lngTimeTook as Long
    lngTimeTook = GetTickCount()
    
    'do some code
    
    lngTimeTook  = GetTickCount() - lngTimeTook 
    
    Msgbox ("This took: " & lngTimeTook & " milisec to do")
    Last edited by stum; Apr 19th, 2015 at 02:26 PM.

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Visual Basic - Code Syntax Speed

    Also you can use Timer



  4. #4
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Visual Basic - Code Syntax Speed

    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Visual Basic - Code Syntax Speed

    while your at, test this... avoiding MOD

    Code:
    x = 0
    For i = 0 To max
      If x = 0 Then 
            dump = 0 
            x = 1
      ElseIf x = 1 Then 
            dump = 1
            x = 2
      Else 
            dump = 2
            x = 0
      End If
    Next
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: Visual Basic - Code Syntax Speed

    The GetTickCount seems to work pretty good for timing to see what VB code run time is almost same as RB elseif was the fastest!

    ElseIf - Fastest - 46 - 47
    Select Case - Second Fastest -62 -63
    If - ReallySlow- 141- 156
    If Else - Same As If - 156- 172

    Code:
    Option Explicit
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    
    Private Sub Command1_Click()
    On Error Resume Next
    Dim start1, start2, start3, start4 As Long
    Dim end1, end2, end3, end4 As Long
    Dim x, i, Max, dump As Integer
    Max = 2000000
    
    start1 = GetTickCount()
    For i = 0 To Max
    If x = 0 Then dump = 0
    If x = 1 Then dump = 1
    If x = 2 Then dump = 2
    Next
    end1 = GetTickCount()
    
    start2 = GetTickCount()
    For i = 0 To Max
      If x = 0 Then
        dump = 0
      ElseIf x = 1 Then
        dump = 1
      ElseIf x = 2 Then
        dump = 2
      End If
    Next i
    end2 = GetTickCount()
    
    start3 = GetTickCount()
    For i = 0 To Max
      Select Case x
      Case 0
        dump = 0
      Case 1
        dump = 1
      Case 2
        dump = 2
      End Select
    Next
    end3 = GetTickCount()
    
    start4 = GetTickCount()
    For i = 0 To Max
    If x = 0 Then dump = 0 Else dump = 0
    If x = 1 Then dump = 1 Else dump = 1
    If x = 2 Then dump = 2 Else dump = 2
    Next
    end4 = GetTickCount()
    
    MsgBox "If  " + CStr(start1 - end1) & vbCrLf & _
     "ElseIf  " + CStr(start2 - end2) & vbCrLf & _
     "Select Case  " + CStr(start3 - end3) & vbCrLf & _
     "If Else  " + CStr(start4 - end4)
    End Sub
    Last edited by sinner0636; Apr 21st, 2015 at 05:04 AM.

  7. #7
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,854

    Re: Visual Basic - Code Syntax Speed

    In VB6 you need to dim all variables one at the time
    In this situation start1, start2 and start3 are Variants
    Code:
    Dim start1, start2, start3, start4 As Long
    Dim end1, end2, end3, end4 As Long
    Dim x, i, Max, dump As Integer
    This is what you want:
    Code:
    Dim start1 As Long, start2 As Long, start3 As Long, start4 As Long
    Dim end1 As Long, end2 As Long, end3 As Long, end4 As Long
    Dim x As Integer, i As Integer, Max As Integer, dump As Integer

  8. #8

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Visual Basic - Code Syntax Speed

    Quote Originally Posted by The trick View Post
    sinner0636, please attach exe-files which were compiled by vb6 and rb. Only you should set the all optimization options in the compiler properties. Let's make up the similary code samples, and i will testing and i will show assembler listing. Ok? Sorry my bad English.
    Exe files are not allowed as attachments

  10. #10

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Visual Basic - Code Syntax Speed

    You should read the forum rules, Binaries are not allowed, Source code only.

  12. #12

  13. #13

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: Visual Basic - Code Syntax Speed

    thanks for the the tip stum and Arnoutdv

  14. #14
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,207

    Re: Visual Basic - Code Syntax Speed

    Quote Originally Posted by sinner0636 View Post
    The GetTickCount seems to work pretty good for timing to see what VB code run time is almost same as RB elseif was the fastest!
    As already mentioned by others, for a true comparison on max achievable speed,
    you would need to:

    - use the VB6-native-Compiler (checking in all the extended Options in the appropriate Dialogue)
    - Remove the active Error-Handler from routines which need to be performed at full-speed (testing them in the IDE beforehand)

    Then the following is the output from a native compiled Executable:



    which had the following Test-Code inside:
    Code:
    Option Explicit
    
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    
    Private Sub Form_Click()
    Dim i As Long, x As Long, start1 As Long, end1 As Long, Max As Long, dump As Long
      Max = 200000000 '<- former Max was 2000000
      
      start1 = GetTickCount()
        For i = 0 To Max
          x = i Mod 10  'put x into a reasonable range (circulating between 0 to 9)
          If x = 0 Then dump = 0
          If x = 1 Then dump = 1
          If x = 2 Then dump = 2
        Next
      end1 = GetTickCount()
    
      Print "If comparisons:  " & (end1 - start1)
    End Sub
    Note, that in the above code, the loop-count (compared to your example) was increased by factor 100!

    @The trick (Anatoli)
    The OP was comparing with RealBasic (RB) not with PowerBasic (PB)


    Olaf

  15. #15

  16. #16
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,207

    Re: [RESOLVED] Visual Basic - Code Syntax Speed

    Quote Originally Posted by The trick View Post
    I just wanted to show that the test does not make sense. The compiler optimizes the expression, and if the variable is not used then it is removed.
    Well, wasn't *that* obvious to me, that the VB6-native compiler was *that* clever in this case,
    so let's just encapsulate the same code in a small function, and return 'dump' as a result.

    Code:
    Option Explicit
    
    Private Sub Form_Click()
    Dim T As Single: T = Timer
      
      Test
      
    Print Format$((Timer - T) * 1000, "0msec")
    End Sub
    
    Private Function Test() As Long
    Dim i As Long, x As Long, dump As Long, Max As Long
    
      Max = 20000000 '<- the OPs Max was 2000000
    
        For i = 0 To Max
          x = i Mod 10  'put x into a reasonable range (circulating between 0 to 9)
          If x = 0 Then dump = 0
          If x = 1 Then dump = 1
          If x = 2 Then dump = 2
        Next
    
      Test = dump
    End Function
    Now it obviously moves through the instructions - since the performance
    is about factor 10 lower now - so I reduced the Max-Loopcount by factor 10.

    Here the screen-shot, which shows the same timing-results as measured before (though as said,
    with a reduced loopcount of 20000000, which is still factor 10 higher than the one the OP was using)



    Think, that this is still a lot faster than what RB achieved on basically the same task
    (FWIW... just wanted to point out a rough performance-measure of a native compiled VB6-binary).

    Olaf

  17. #17

Posting Permissions

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



Click Here to Expand Forum to Full Width