Results 1 to 5 of 5

Thread: Which Is Faster: Assignment or Comparison

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Which Is Faster: Assignment or Comparison

    Which of these is faster?
    1)
    VB Code:
    1. If a_long_set_of_conditions Then a_variable = False

    2)
    VB Code:
    1. a_variable = Not a_long_set_of_conditions
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Which Is Faster: Assignment or Comparison

    According to a test I just ran (see the GetTickCount link in my signature) this

    a_variable = Not (a And b And c)

    takes about twice as long as

    If a Or b Or c Then a_variable = False

  3. #3

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Which Is Faster: Assignment or Comparison

    Thanks Marty.

    I need to learn how to setup my own benchmarks. Not that its hard or anything, I just have no experience in it and always talk myself out of it. I suppose the link in your sig is a good place to start.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  4. #4

  5. #5

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Which Is Faster: Assignment or Comparison

    Hmmm. Here is the code I used:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetTickCount Lib "kernel32" () As Long
    4.  
    5. Private Sub Command1_Click()
    6.     Dim i           As Long
    7.     Dim startTime   As Long
    8.     Dim stopTime    As Long
    9.     Dim timesToLoop As Long
    10.    
    11.     Dim time1       As Long
    12.     Dim time2       As Long
    13.    
    14.     Dim glToGo     As Long
    15.     Dim bContinueLoop   As Boolean
    16.    
    17.     Dim mcrdLastFound1  As Long
    18.     Dim mcrdLastFound2  As Long
    19.    
    20.     Dim crd1            As Long
    21.     Dim crd2            As Long
    22.    
    23.     mcrdLastFound1 = 9
    24.     mcrdLastFound2 = 3
    25.    
    26.     crd1 = 1
    27.     crd2 = 6
    28.    
    29.     bContinueLoop = True
    30.    
    31.     glToGo = 1
    32.    
    33.     timesToLoop = 20000000
    34.    
    35.     Me.MousePointer = vbArrowHourglass
    36.    
    37.     startTime = GetTickCount
    38.         For i = 1 To timesToLoop
    39.             bContinueLoop = Not (glToGo = 0 Or (mcrdLastFound1 = crd1 And mcrdLastFound2 = crd2))
    40.         Next i
    41.     stopTime = GetTickCount
    42.     time1 = stopTime - startTime
    43.    
    44.     startTime = GetTickCount
    45.         For i = 1 To timesToLoop
    46.             If glToGo = 0 Or (mcrdLastFound1 = crd1 And mcrdLastFound2 = crd2) Then bContinueLoop = False
    47.         Next i
    48.     stopTime = GetTickCount
    49.     time2 = stopTime - startTime
    50.    
    51.     Me.MousePointer = vbArrow
    52.    
    53.     Text1.Text = Text1.Text & time1 & vbCrLf
    54.     Text2.Text = Text2.Text & time2 & vbCrLf
    55. End Sub
    I got these results:
    First Way:
    3359
    3437
    3359
    3422

    Second Way:
    3094
    3094
    3172
    3109


    But if I set glToGo = 0 then I get these results:
    First Way:
    3203
    3343
    3125
    3125

    Second Way:
    4047
    3860
    3875
    3891


    It must have something to do with the Or's
    Attached Files Attached Files
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

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