|
-
Jul 20th, 2010, 07:14 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Efficiency Comparison of Nested Ifs ??
Hi,
A general question: which is more efficient?
(Assume case statements, etc., cannot be used.)
Code:
If a Then
If x Then
ax()
ElseIf y Then
ay()
End If
ElseIf b Then
If x Then
bx()
ElseIf y Then
by()
End If
ElseIf c Then
If x Then
cx()
ElseIf y Then
cy()
End If
End If
-- or --
Code:
If a AndAlso x Then
ax()
ElseIf a AndAlso y Then
ay()
ElseIf b AndAlso x Then
bx()
ElseIf b AndAlso y Then
by()
ElseIf c AndAlso x Then
cx()
ElseIf c AndAlso y Then
cy()
End If
-
Jul 20th, 2010, 07:36 PM
#2
Re: Efficiency Comparison of Nested Ifs ??
Easiest way to find out: place a stopwatch at the beginning and end of each type and time them.
-
Jul 21st, 2010, 11:56 AM
#3
Thread Starter
Addicted Member
Re: Efficiency Comparison of Nested Ifs ??
Yeah. Why didn't I just time it?
The AndAlso-ed code is >20% faster than the nested code.
Nested AndAlso-ed Diff
1 421.8615 359.3635 62.498
2 437.486 328.1145 109.3715
3 437.486 328.1145 109.3715
4 390.6125 328.1145 62.498
5 406.237 343.739 62.498
6 406.237 328.1145 78.1225
7 390.6125 343.739 46.8735
8 390.6125 328.1145 62.498
9 406.237 328.1145 78.1225
10 390.6125 328.1145 62.498
11 421.8615 328.1145 93.747
12 437.486 343.739 93.747
ave 411.4451667 334.6247083 76.82045833
Faster by 22.95719844 %
(Hmm..., tables don't paste well.)
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a, b, c, x, y As Boolean
Dim i As Integer
Dim d As Date
d = Now
For i = 0 To 12000000
If i = 3000000 Then c = True
If i = 6000000 Then b = True
If i = 9000000 Then a = True
If i = 1000000 Then y = True
If i = 2000000 Then x = True
If i = 3000000 Then x = False : y = False
If i = 4000000 Then y = True
If i = 5000000 Then x = True
If i = 6000000 Then x = False : y = False
If i = 7000000 Then y = True
If i = 8000000 Then x = True
If i = 9000000 Then x = False : y = False
If i = 10000000 Then y = True
If i = 11000000 Then x = True
If a Then
If x Then
ss()
ElseIf y Then
ss()
End If
ElseIf b Then
If x Then
ss()
ElseIf y Then
ss()
End If
ElseIf c Then
If x Then
ss()
ElseIf y Then
ss()
End If
End If
Next
TextBox1.Text &= (Now - d).TotalMilliseconds.ToString & vbCrLf
d = Now
For i = 0 To 12000000
If i = 3000000 Then c = True
If i = 6000000 Then b = True
If i = 9000000 Then a = True
If i = 1000000 Then y = True
If i = 2000000 Then x = True
If i = 3000000 Then x = False : y = False
If i = 4000000 Then y = True
If i = 5000000 Then x = True
If i = 6000000 Then x = False : y = False
If i = 7000000 Then y = True
If i = 8000000 Then x = True
If i = 9000000 Then x = False : y = False
If i = 10000000 Then y = True
If i = 11000000 Then x = True
If a AndAlso x Then
ss()
ElseIf a AndAlso y Then
ss()
ElseIf b AndAlso x Then
ss()
ElseIf b AndAlso y Then
ss()
ElseIf c AndAlso x Then
ss()
ElseIf c AndAlso y Then
ss()
End If
Next
TextBox2.Text &= (Now - d).TotalMilliseconds.ToString & vbCrLf
End Sub
Sub ss()
End Sub
-
Jul 21st, 2010, 12:25 PM
#4
Re: Efficiency Comparison of Nested Ifs ??
-
Jul 21st, 2010, 12:29 PM
#5
Thread Starter
Addicted Member
Re: [RESOLVED] Efficiency Comparison of Nested Ifs ??
Yeah, its resolved. I will mark it as such.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|