Results
Lock: 85.3 microseconds
Monitor.TryEnter: 11.0 microseconds
Isn\'t the lock expanded into the same code?
Edit: Results with 1000 iteratio
I don't actually know the answer, but feel it's important to point out that lock
and Monitor.TryEnter
are not functionally equivalent. From the MSDN documentation on Monitor.TryEnter:
If successful, this method acquires an exclusive lock on the obj parameter. This method returns immediately, whether or not the lock is available.
The lock
statement is analogous to Monitor.Enter
, which does potentially block. Granted, in your example code, there shouldn't be any blocking issues; but I would wager that since lock
provides blocking, it does a little more work (potentially) than TryEnter
does.
For what it's worth, I just tried your code on my machine and got completely different results:
100 iterations:
lock
: 4.4 microseconds
Monitor.TryEnter
: 16.1 microseconds
Monitor.Enter
: 3.9 microseconds
100000 iterations:
lock
: 2872.5 microseconds
Monitor.TryEnter
: 5226.6 microseconds
Monitor.Enter
: 2432.9 microseconds
This seriously undermines my initial guess and shows that, on my system, lock
(which performs about the same as Monitor.Enter
) actually drastically outperforms Monitor.TryEnter
.
Indeed, I attempted this in VS 2010 targeting both .NET 3.5 and .NET 4.0 and, though the results were different, in each case lock
did in fact outperform Monitor.TryEnter
:
Ran 100 times, 100000 iterations each time:
Lock: 279736.4 microseconds
Monitor.TryEnter: 1366751.5 microseconds
Monitor.TryEnter (no timeout): 475107.3 microseconds
Monitor.Enter: 332334.1 microseconds
Ran 100 times, 100000 iterations each time:
Lock: 334273.7 microseconds
Monitor.TryEnter: 1671363.4 microseconds
Monitor.TryEnter (no timeout): 531451.8 microseconds
Monitor.Enter: 316693.1 microseconds
(Notice I also tested Monitor.TryEnter
with no timeout, as I agreed with Marc that calling TimeSpan.FromSeconds
was almost certainly slowing down your times for Monitor.TryEnter
--and these tests support that--though it's strange, since in your case apparently lock
is still significantly slower.)
Based on these results I am strongly inclined to believe that your measured execution times are somehow affected by running this code with the Test
attribute. Either that or this code is far more machine-dependent than I would have expected.