If you have any doubts about that, it’s good to have a little test app where you can try things out. A simple WinForms application with a single form will do. Put a pair of method stubs on there along with a stopwatch object for timing. I like to run method 1, then method 2, then method 2, and finally method 1, so each gets called twice. That almost never makes any difference, but you don’t want to call each one just once, because you’ll see that there’s a surprising amount of variability in timing. I know another person who runs each method three or four times and gets an average. I prefer just eyeballing the results.
As to those results, I have labels to show the elapsed milliseconds for each method, so four labels.
Inside the stubs, you can put whatever you want. In this case, you might put the If statement in one and make the other one identical except for the If statement. What you’ll find, though is that you’ll have to put them into a loop, and run the loop thousands or tens of thousands of times to get the difference high enough so that you can see the difference. For example, here’s what I happened to have in my test bed project currently:
Private Sub test1()
Dim x As Integer
Dim n As Long = 0
Dim lGD As TestGUID = Nothing
Dim a As Integer = 1
Dim b As Integer = 1
Dim c As Integer = 1
For x = 0 To 10
Using cn As New SqlClient.SqlConnection(mConString)
Using cmd As SqlClient.SqlCommand = cn.CreateCommand
cn.Open()
cmd.CommandText = "SELECT * FROM GEN_Configuration"
Dim obj = cmd.ExecuteScalar
End Using
End Using
Next
End Sub
Private Sub test2()
Dim x As Integer
Dim n As Long = 0
Dim lGD As TestGUID = Nothing
Dim a As Integer = 1
Dim b As Integer = 1
Dim c As Integer = 1
Using cn As New SqlClient.SqlConnection(mConString)
Using cmd As SqlClient.SqlCommand = cn.CreateCommand
cn.Open()
For x = 0 To 10
cmd.CommandText = "SELECT * FROM GEN_Configuration"
Dim obj = cmd.ExecuteScalar
Next
End Using
End Using
End Sub
I remember what this was about. Somebody had asked whether opening the connection once and performing a bunch of queries was noticeably more efficient than opening a connection once for each query, then getting rid of it. In this case, since there was a SQL query involved, I only had to do 10 iterations to be able to see a difference, but if you looked at something like the cost of the If statement, you’d probably need to do tens of thousands of iterations for the difference to become large enough to notice.
In general, you’re right to be suspicious of If statements. Since the Pentium processor, conditionals can have an unpredictable impact on performance, but in all cases…it’s pretty modest, these days.