Which of these is faster?
1)VB Code:
If a_long_set_of_conditions Then a_variable = False
2)VB Code:
a_variable = Not a_long_set_of_conditions
Printable View
Which of these is faster?
1)VB Code:
If a_long_set_of_conditions Then a_variable = False
2)VB Code:
a_variable = Not a_long_set_of_conditions
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
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. ;)
Yup.Quote:
Originally Posted by eyeRmonkey
Hmmm. Here is the code I used:
I got these results:VB Code:
Option Explicit Private Declare Function GetTickCount Lib "kernel32" () As Long Private Sub Command1_Click() Dim i As Long Dim startTime As Long Dim stopTime As Long Dim timesToLoop As Long Dim time1 As Long Dim time2 As Long Dim glToGo As Long Dim bContinueLoop As Boolean Dim mcrdLastFound1 As Long Dim mcrdLastFound2 As Long Dim crd1 As Long Dim crd2 As Long mcrdLastFound1 = 9 mcrdLastFound2 = 3 crd1 = 1 crd2 = 6 bContinueLoop = True glToGo = 1 timesToLoop = 20000000 Me.MousePointer = vbArrowHourglass startTime = GetTickCount For i = 1 To timesToLoop bContinueLoop = Not (glToGo = 0 Or (mcrdLastFound1 = crd1 And mcrdLastFound2 = crd2)) Next i stopTime = GetTickCount time1 = stopTime - startTime startTime = GetTickCount For i = 1 To timesToLoop If glToGo = 0 Or (mcrdLastFound1 = crd1 And mcrdLastFound2 = crd2) Then bContinueLoop = False Next i stopTime = GetTickCount time2 = stopTime - startTime Me.MousePointer = vbArrow Text1.Text = Text1.Text & time1 & vbCrLf Text2.Text = Text2.Text & time2 & vbCrLf End Sub
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