A coworker and I have differing opinions on If
statements and their performance. My view is that If...ElseIf
statements should be used. His view is that he doesn't believe in ElseIf
, and writes everything with nested If
statements.
Let's assume that a case statement cannot be used in this situation. What I am wondering is how efficiently code will be executed using nested If..Else
statements versus using If...ElseIf
statements. I know that code readability is a factor, but that shouldn't' affect performance.
Lets look at the following examples.
Using If Else:
If () then
'Do something'
Else
If () then
'Do something'
Else
If () then
'Do something'
Else
If () then
'Do something'
Else
'Do something else'
End If
End If
End If
End If
Using ElseIf:
If () then
'Do something'
ElseIf () then
'Do something'
ElseIf () then
'Do something'
ElseIf () then
'Do something'
Else
'Do something else'
End If
I know this is a small scale example, but lets say blocks like this are used heavily throughout the application.
Are there any performance differences between the two code sections, or would they perform almost identically once the application is compiled?
####UPDATE#####
I created a program to test running through the functions x number of times.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 1000
Run()
Next
End Sub
Sub Run()
Dim Time1Start As Integer = 0
Dim Time1End As Integer = 0
Dim Time2Start As Integer = 0
Dim Time2End As Integer = 0
Time2Start = CInt(DateTime.Now.ToString("fff"))
runElse()
Time2End = CInt(DateTime.Now.ToString("fff"))
Time1Start = CInt(DateTime.Now.ToString("fff"))
runElseIf()
Time1End = CInt(DateTime.Now.ToString("fff"))
TextBox1.Text += If(Time1End < Time1Start, Time1End + (1000 - Time1Start), Time1End - Time1Start) & vbTab & If(Time2End < Time2Start, Time2End + (1000 - Time2Start), Time2End - Time2Start) & vbCrLf
End Sub
Sub runElseIf()
If sleep(10) Then
'Do something'
Else
If sleep(10) Then
'Do something'
Else
If sleep(10) Then
'Do something'
Else
If sleep(10) Then
'Do something'
Else
If sleep(10) Then
'Do something'
Else
If sleep(10) Then
'Do something'
Else
If sleep(10) Then
'Do something'
Else
If sleep(10) Then
'Do something'
Else
If sleep(10) Then
'Do something'
Else
If sleep(10) Then
'Do something'
Else
'Do something else'
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End Sub
Sub runElse()
If sleep(10) Then
'Do something'
ElseIf sleep(10) Then
'Do something'
ElseIf sleep(10) Then
'Do something'
ElseIf sleep(10) Then
'Do something'
ElseIf sleep(10) Then
'Do something'
ElseIf sleep(10) Then
'Do something'
ElseIf sleep(10) Then
'Do something'
ElseIf sleep(10) Then
'Do something'
ElseIf sleep(10) Then
'Do something'
ElseIf sleep(10) Then
'Do something'
Else
'Do something else'
End If
End Sub
Function sleep(ByVal ms As Integer) As Integer
System.Threading.Thread.Sleep(ms)
Return False
End Function
End Class
I ran the program and here are my results:
500 Loops Average - ElseIf: 108.248ms If Else: 106.507ms
1000 Loops Average - ElseIf: 107.747ms If Else: 107.451ms (Else If running first)
1000 Loops Average - ElseIf: 107.683ms If Else: 107.076ms (ElseIf running first)
Perhaps with a larger data set the numbers would change, but out of those 3 trials it actually appears that the If Else
is outperforming the ElseIf
statements.