このような例を書いて、非同期実行の BlockingCollection の速さを測定しました
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace TestBlockingCollection
{
class Program
{
static void Main(string[] args)
{
BlockingCollection<int> blockingCollection = new BlockingCollection<int>();
Stopwatch sw = Stopwatch.StartNew();
Task.Factory.StartNew(() =>
{
int i = 0;
while (true)
{
Console.WriteLine("Adding " + i);
sw = Stopwatch.StartNew();
blockingCollection.Add(i++);
Thread.Sleep(1000);
}
});
Task.Factory.StartNew(() =>
{
while (true)
{
int i = blockingCollection.Take();
sw.Stop();
long microseconds = sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));
Console.WriteLine("Received " + i + ". Spent " + microseconds + " microseconds.");
}
});
while(true)
{
Thread.Sleep(1000);
}
}
}
}
結果は残念です:
Adding 0
Received 0. Spent 19593 microseconds.
Adding 1
Received 1. Spent 220 microseconds.
Adding 2
Received 2. Spent 38 microseconds.
Adding 3
Received 3. Spent 104 microseconds.
Adding 4
Received 4. Spent 46 microseconds.
Adding 5
Received 5. Spent 37 microseconds.
Adding 6
Received 6. Spent 112 microseconds.
Adding 7
Received 7. Spent 103 microseconds.
Adding 8
Received 8. Spent 104 microseconds.
Adding 9
Received 9. Spent 384 microseconds.
Adding 10
Received 10. Spent 102 microseconds.
Adding 11
Received 11. Spent 39 microseconds.
Adding 12
Received 12. Spent 51 microseconds.
Adding 13
Received 13. Spent 42 microseconds.
Adding 14
Received 14. Spent 40 microseconds.
Adding 15
Received 15. Spent 40 microseconds.
Adding 16
Received 16. Spent 42 microseconds.
Adding 17
Received 17. Spent 40 microseconds.
Adding 18
Received 18. Spent 41 microseconds.
Adding 19
Received 19. Spent 42 microseconds.
Adding 20
Received 20. Spent 62 microseconds.
Adding 21
Received 21. Spent 36 microseconds.
Adding 22
Received 22. Spent 39 microseconds.
Adding 23
Received 23. Spent 35 microseconds.
Adding 24
Received 24. Spent 40 microseconds.
Adding 25
Received 25. Spent 63 microseconds.
Adding 26
Received 26. Spent 56 microseconds.
Adding 27
Received 27. Spent 42 microseconds.
Adding 28
Received 28. Spent 41 microseconds.
Adding 29
Received 29. Spent 42 microseconds.
Adding 30
Received 30. Spent 41 microseconds.
Adding 31
Received 31. Spent 651 microseconds.
Adding 32
Received 32. Spent 43 microseconds.
Adding 33
Received 33. Spent 58 microseconds.
Adding 34
Received 34. Spent 43 microseconds.
Adding 35
Received 35. Spent 41 microseconds.
Adding 36
Received 36. Spent 59 microseconds.
Adding 37
Received 37. Spent 38 microseconds.
Adding 38
Received 38. Spent 38 microseconds.
Adding 39
Received 39. Spent 38 microseconds.
Adding 40
Received 40. Spent 42 microseconds.
Adding 41
Received 41. Spent 59 microseconds.
Adding 42
Received 42. Spent 40 microseconds.
Adding 43
Received 43. Spent 42 microseconds.
Adding 44
Received 44. Spent 41 microseconds.
Adding 45
Received 45. Spent 39 microseconds.
Adding 46
Received 46. Spent 42 microseconds.
Adding 47
Received 47. Spent 41 microseconds.
Adding 48
Received 48. Spent 41 microseconds.
Adding 49
Received 49. Spent 42 microseconds.
Adding 50
Received 50. Spent 35 microseconds.
Adding 51
Received 51. Spent 42 microseconds.
Adding 52
Received 52. Spent 39 microseconds.
Adding 53
Received 53. Spent 43 microseconds.
Adding 54
Received 54. Spent 35 microseconds.
Adding 55
Received 55. Spent 60 microseconds.
Adding 56
Received 56. Spent 59 microseconds.
Adding 57
Received 57. Spent 55 microseconds.
Adding 58
Received 58. Spent 74 microseconds.
Adding 59
Received 59. Spent 56 microseconds.
Adding 60
Received 60. Spent 42 microseconds.
平均して約 50 マイクロ秒を費やしましたが、最大で 600 マイクロ秒を費やしたこともあります。
遅い Pentium U5400 を使用しても、数マイクロ秒、最大 10 マイクロ秒、決して最大 10 マイクロ秒の定数になるはずです。
.NET には、非同期実行のためのより高速な方法がありますか? async exec がスケジュールされた後、できるだけ早く開始する必要があります。これは、金融の時間に敏感な計算です。
コレクションをブロックすると、順序が保証され、アイテムが 1 つずつ処理されることが保証されるため、この質問には実際には 2 つの質問が含まれます
- 注文が必要で、アイテムが表示された順序で処理される必要がある場合、より速いものはありますか? つまり、FIFO クエリが必要です。
- 順序を気にせず、アイテムが 1 つずつ処理されるか、並行して処理されるかを気にしない場合、より速いものはありますか?
答えは次のとおりだと思います。
いいえ。その参照には BlockingCollection を使用する必要があります。BlockingCollection<T> をシングル プロデューサー、シングル コンシューマーの FIFO クエリとして使用するのは適切ですか?
おそらくデリゲートを試すことができますか?http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx