how to execute 50 parallel methods 500 times per second effectively?
<p>I need to execute <code>strategy.AllTablesUpdated();</code> for 50 strategies in 2 ms (and I need to repeat that ~500 times per second).
Using code below I discovered that just <code>Monitor.TryEnter</code> call spents up to <strong>1 ms</strong> (!!!) and I do that 50 times!</p>

<pre><code>    // must be called ~500 times per second
    public void FinishUpdatingTables()
    {
        foreach (Strategy strategy in strategies)   // about ~50, should be executed in 2 ms
        {
            // this slow and can be paralleled
            strategy.AllTablesUpdated();
        }
    }
</code></pre>

<p>...................</p>

<pre><code>    public override bool AllTablesUpdated(Stopwatch sw)
    {
        this.sw = sw;
        Checkpoint(this + " TryEnter attempt ");
        if (Monitor.TryEnter(desiredOrdersBuy))
        {
            Checkpoint(this + " TryEnter success ");
            try
            {
                OnAllTablesUpdated();
            } finally
            {
                Monitor.Exit(desiredOrdersBuy);
            }
            return true;
        } else
        {
            Checkpoint(this + " TryEnter failed ");
        }
        return false;
    }

    public void Checkpoint(string message)
    {
        if (sw == null)
        {
            return;
        }
        long time = sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));
        Log.Push(LogItemType.Debug, message + time);
    }
</code></pre>

<p>From logs (in µs), failed attempt spents ~ 1ms:</p>

<blockquote>
  <blockquote>
    <p>12:55:43:778 Debug: TryEnter attempt 1264
    12:55:43:779 Debug: TryEnter failed 2123</p>
  </blockquote>
</blockquote>

<p>From logs (in µs), succeed attempt spents ~ 0.01ms:</p>

<blockquote>
  <blockquote>
    <p>12:55:49:701 Debug: TryEnter attempt 889
    12:55:49:701 Debug: TryEnter success 900</p>
  </blockquote>
</blockquote>

<p>So now I think that <code>Monitor.TryEnter</code> is too expensive for me to be executed one by one for 50 strategies. So I want to parallel this work using <code>Task</code> like that:</p>

<pre><code>    // must be called ~500 times per second
    public void FinishUpdatingTables()
    {
        foreach (Strategy strategy in strategies)  // about ~50, should be executed in 2 ms
        {
            // this slow and can be paralleled
            Task.Factory.StartNew(() =&gt; {
                strategy.AllTablesUpdated();
            });
        }
    }
</code></pre>

<p>I will also probably replace <code>Monitor.TryEnter</code> to just <code>lock</code> as with such approach everything will be asynchronous.</p>

<p>My questions:</p>

<ul>
<li>Why <code>Monitor.TryEnter</code> is so slow ? (1 ms if lock is not obtained)</li>
<li>How good would be to start 50 <code>Task</code> each 2 ms = 25 000 of Tasks each second? Can .NET manage this effectively? I can also use producer-consumer pattern with <code>BlockingCollection</code> and start 50 "workers" only ONCE and then submit new pack of 50 items each 2 ms to BlockingCollection? Would that be better?</li>
<li>How would you execute 50 methods that can be paralleled each 2 ms (500 times per second), totally 25 000 times per second?</li>
</ul>
