我需要在2毫秒內執行strategy.AllTablesUpdated();
50個策略(我需要重複每秒〜500次)。 使用下面的代碼我發現,只是Monitor.TryEnter
呼叫拼寫高達1毫秒(!!!),我做了50次!如何有效地執行每秒500次並行方法500次?
// 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();
}
}
...................
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);
}
從日誌(以微秒),失敗的嘗試用時1毫秒〜:
12:55:43:778調試:TryEnter嘗試1264 12:55:43:779調試:TryEnter失敗2123
從日誌(以μs爲單位),成功嘗試用時〜0.01ms:
12:55:49:701調試:TryEnter嘗試889 12:55:49:701調試:TryEnter成功900
所以現在我認爲Monitor.TryEnter
是太貴了,我一個50個戰略執行一個。所以我想利用平行像Task
這項工作:
// 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(() => {
strategy.AllTablesUpdated();
});
}
}
我也可能會取代Monitor.TryEnter
只是lock
與這樣的做法一切都將是異步的。
我的問題:
- 爲什麼
Monitor.TryEnter
是如此之慢? (如果沒有獲得鎖,則爲1毫秒) - 啓動50
Task
每秒2毫秒= 25,000任務每秒? .NET可以有效地管理這個嗎?我也可以使用BlockingCollection
生產者 - 消費者模式,並且只開始50個「工作人員」,然後每2毫秒向BlockingCollection提交50個物品的新包裝?這會更好嗎? - 你將如何執行50個方法,每個方法可以並行2 ms(每秒500次),每秒總共25 000次?
如果您使用任務,它將花費超過2ms,因爲任務不會立即開始。 – 2012-04-18 09:52:08