K
Keith Langer
I would like to know why locking on a new object takes significantly
longer than locking on a strongly typed object. All of the posts I've
seen say that it is best to lock on a "new object", but it takes more
than 3x longer than locking on anything other than object. Here's a
test case:
Module Module1
Sub Main()
Dim T As clsTest
Dim i As Integer
Dim D As Date
Const MAX = 10000000
Dim LL As New Object
Dim DTest As Date
Dim T2 As New clsDummy
T = New clsTest
D = Now
For i = 1 To MAX
T.Index += 1
Next
Console.WriteLine("No lock: " & Now.Ticks - D.Ticks)
T = New clsTest
D = Now
For i = 1 To MAX
SyncLock T
T.Index += 1
End SyncLock
Next
Console.WriteLine("Lock on owner: " & Now.Ticks - D.Ticks)
T = New clsTest
D = Now
For i = 1 To MAX
SyncLock LL
T.Index += 1
End SyncLock
Next
Console.WriteLine("Lock on new object: " & Now.Ticks - D.Ticks)
T = New clsTest
D = Now
For i = 1 To MAX
SyncLock T2
T.Index += 1
End SyncLock
Next
Console.WriteLine("Lock on Dummy object: " & Now.Ticks -
D.Ticks)
Console.ReadLine()
End Sub
Class clsDummy
End Class
Class clsTest
Dim m_Index As Integer
Public Property Index() As Integer
Get
Return m_Index
End Get
Set(ByVal Value As Integer)
m_Index += 1
End Set
End Property
End Class
thanks,
Keith
longer than locking on a strongly typed object. All of the posts I've
seen say that it is best to lock on a "new object", but it takes more
than 3x longer than locking on anything other than object. Here's a
test case:
Module Module1
Sub Main()
Dim T As clsTest
Dim i As Integer
Dim D As Date
Const MAX = 10000000
Dim LL As New Object
Dim DTest As Date
Dim T2 As New clsDummy
T = New clsTest
D = Now
For i = 1 To MAX
T.Index += 1
Next
Console.WriteLine("No lock: " & Now.Ticks - D.Ticks)
T = New clsTest
D = Now
For i = 1 To MAX
SyncLock T
T.Index += 1
End SyncLock
Next
Console.WriteLine("Lock on owner: " & Now.Ticks - D.Ticks)
T = New clsTest
D = Now
For i = 1 To MAX
SyncLock LL
T.Index += 1
End SyncLock
Next
Console.WriteLine("Lock on new object: " & Now.Ticks - D.Ticks)
T = New clsTest
D = Now
For i = 1 To MAX
SyncLock T2
T.Index += 1
End SyncLock
Next
Console.WriteLine("Lock on Dummy object: " & Now.Ticks -
D.Ticks)
Console.ReadLine()
End Sub
Class clsDummy
End Class
Class clsTest
Dim m_Index As Integer
Public Property Index() As Integer
Get
Return m_Index
End Get
Set(ByVal Value As Integer)
m_Index += 1
End Set
End Property
End Class
thanks,
Keith