Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't actually know the answer, but feel it's important to point out that <code>lock</code> and <code>Monitor.TryEnter</code> are <strong>not</strong> functionally equivalent. From <a href="http://msdn.microsoft.com/en-us/library/4tssbxcw.aspx" rel="noreferrer">the MSDN documentation on <code>Monitor.TryEnter</code></a>:</p> <blockquote> <p>If successful, this method acquires an exclusive lock on the obj parameter. This method returns immediately, whether or not the lock is available.</p> </blockquote> <p>The <code>lock</code> statement is analogous to <code>Monitor.Enter</code>, which <em>does</em> potentially block. Granted, in your example code, there shouldn't be any blocking issues; but I would wager that since <code>lock</code> provides blocking, it does a little more work (potentially) than <code>TryEnter</code> does.</p> <hr> <p>For what it's worth, I just tried your code on my machine and got <strong>completely</strong> different results:</p> <p>100 iterations:<br> <code>lock</code>: 4.4 microseconds<br> <code>Monitor.TryEnter</code>: 16.1 microseconds<br> <code>Monitor.Enter</code>: 3.9 microseconds</p> <p>100000 iterations:<br> <code>lock</code>: 2872.5 microseconds<br> <code>Monitor.TryEnter</code>: 5226.6 microseconds<br> <code>Monitor.Enter</code>: 2432.9 microseconds</p> <p>This seriously undermines my initial guess and shows that, on my system, <code>lock</code> (which performs about the same as <code>Monitor.Enter</code>) actually drastically <em>outperforms</em> <code>Monitor.TryEnter</code>.</p> <hr> <p>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 <code>lock</code> did in fact outperform <code>Monitor.TryEnter</code>:</p> <h2>Runtime version: 2.0.50727.3603</h2> <p>Ran 100 times, 100000 iterations each time:<br> <strong>Lock: 279736.4</strong> microseconds<br> <strong>Monitor.TryEnter: 1366751.5</strong> microseconds<br> Monitor.TryEnter (no timeout): 475107.3 microseconds<br> Monitor.Enter: 332334.1 microseconds</p> <h2>Runtime version: 4.0.30128.1</h2> <p>Ran 100 times, 100000 iterations each time:<br> <strong>Lock: 334273.7</strong> microseconds<br> <strong>Monitor.TryEnter: 1671363.4</strong> microseconds<br> Monitor.TryEnter (no timeout): 531451.8 microseconds<br> Monitor.Enter: 316693.1 microseconds</p> <p>(Notice I also tested <code>Monitor.TryEnter</code> with no timeout, as I agreed with Marc that calling <code>TimeSpan.FromSeconds</code> was almost certainly slowing down your times for <code>Monitor.TryEnter</code>--and these tests support that--though it's strange, since in <em>your</em> case apparently <code>lock</code> is <em>still</em> significantly slower.)</p> <p>Based on these results I am strongly inclined to believe that your measured execution times are somehow affected by running this code with the <code>Test</code> attribute. Either that or this code is far more machine-dependent than I would have expected.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload