O
Oliver Corona
I am wondering if anyone has any insights on the performance benefit (or
detriment) of declaring local variables instead of referencing members. Is
allocating memory for a new variable more efficient than repeatedly
referencing the member in a loop?
Maybe using a string isn't the best example, but hopefully you get the
idea!
* example (referencing member):
Dim s As String = "this is a test"
For i As Integer = 0 To s.Length
If s.Chars(i) = "x"c Then
Console.WriteLine(s.Chars(i))
End If
Next i
* example (creating new variable):
Dim s As String = "this is a test"
Dim chars As Char() = s.Chars
For i As Integer = 0 To s.Length
If chars(i) = "x"c Then
Console.WriteLine(chars(i))
End If
Next i
detriment) of declaring local variables instead of referencing members. Is
allocating memory for a new variable more efficient than repeatedly
referencing the member in a loop?
Maybe using a string isn't the best example, but hopefully you get the
idea!
* example (referencing member):
Dim s As String = "this is a test"
For i As Integer = 0 To s.Length
If s.Chars(i) = "x"c Then
Console.WriteLine(s.Chars(i))
End If
Next i
* example (creating new variable):
Dim s As String = "this is a test"
Dim chars As Char() = s.Chars
For i As Integer = 0 To s.Length
If chars(i) = "x"c Then
Console.WriteLine(chars(i))
End If
Next i