Dennis,
| I did
| this for 10m cycles and found very little difference then upped it to 100m
| cycles and found that reading/writing to a field is pretty consistently
120
| seconds faster than reading/writing a property on my computer.
I would expect your results to be closer to zero, as there is a good chance
the JIT will inline the property Getter & Setter.
http://msdn.microsoft.com/library/de...anagedapps.asp
Did you test the Debug or Release build of your app?
Did you test from a command prompt or from Visual Studio?
If you tested the Debug build from Visual Studio, then the JIT will not
attempt to optimize the code. However if you tested the Release build from
the command line then the JIT will attempt to "fully" optimize the code.
Your numbers suggests you tested a Debug build, possible under Visual
Studio!
Try the following from a command prompt for both Debug & Release builds:
Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef X As
Long) As Short
Declare Function QueryPerformanceFrequency Lib "Kernel32" (ByRef X As
Long) As Short
Private Class Person
Public m_name As String
Public Sub New(ByVal name As String)
m_name = name
End Sub
Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
End Set
End Property
End Class
Public Shared Sub Main()
Const MaxCount As Integer = 1000000
Const format As String = "{1}: {0}"
Dim aPerson As New Person("Jay")
For outerCount As Integer = 1 To 5
Dim start, finish, frequency As Long
QueryPerformanceFrequency(frequency)
QueryPerformanceCounter(start)
For innerCount As Integer = 1 To MaxCount
aPerson.Name = "Fred"
Next
QueryPerformanceCounter(finish)
Console.WriteLine(format, TimeSpan.FromSeconds((finish - start)
/ frequency), "Property")
QueryPerformanceCounter(start)
For innerCount As Integer = 1 To MaxCount
aPerson.m_name = "Fred"
Next
QueryPerformanceCounter(finish)
Console.WriteLine(format, TimeSpan.FromSeconds((finish - start)
/ frequency), "Field")
Next
End Sub
For Debug builds you should see Property much larger then Field. For Release
builds you should see both numbers nearly the same.
Hope this helps
Jay
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:FE**********************************@microsof t.com...
| As you can see that from the responses, people on this newsgroup don't
like
| fields but are stuck on properties. I find fields useful in certain
| situations and would suggest you use whatever is best for you. As for
the
| speed, I wrote a test program to read and write to both a string field and
a
| stirng property where the property was stored as a private variable. I
did
| this for 10m cycles and found very little difference then upped it to 100m
| cycles and found that reading/writing to a field is pretty consistently
120
| seconds faster than reading/writing a property on my computer.
| --
| Dennis in Houston
|
|
| "Stefan De Schepper" wrote:
|
| > Should I use:
| >
| > Private m_Name As String
| >
| > Public Property Name() As String
| > Get
| > Return m_Name
| > End Get
| > Set(ByVal Value As String)
| > m_Name = Value
| > End Set
| > End Property
| >
| > or just
| >
| > Public Name As String
| >
| > The last one just has to be faster, or am I wrong?
| >
| > Stefan
| >
| >
| >