Reputation: 24067
I wrote such example to measure how fast BlockingCollection for asynchronous execution
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);
}
}
}
}
Results are disappointing:
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.
In average I spent about 50 microseconds, but sometimes I spent up to 600 microseconds!
Even using my slow Pentium U5400 I expect that should be constant of several, not more than 10, and never more than 10 microseconds.
What faster method .NET has for async exec? After async exec scheduled I need it to start asap. This is financical time-sensitive calculations.
Blocking collection garantees order and garantees that items will be processed one by one, so this question contains actually two question
I guess that answers are:
No. I have to use BlockingCollection for that refer is it good to use BlockingCollection<T> as single-producer, single-consumer FIFO query?
I can probably try delegates? http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx
Upvotes: 1
Views: 557
Reputation: 19403
After a good few hours of experimentation I think it's the measurement that's getting in the way; I changed it to run for 10000 and the results are:
Total 214.0 for 100000 iterations, average 0.00214 ms
The code is pretty much as yours (it's below for reference); I did do a release build
I experimented with using a Barrier but it was slower. I also tried to do it using just locks but failed to get that working.
static void Main(string[] args)
{
var barrier = new Barrier(2);
var collection = new List<int>();
var num_iterations = 100000;
var iterations = num_iterations;
var total_time_ms = 0.0M;
Stopwatch sw = new Stopwatch();
total_time_ms = 0.0M;
iterations = num_iterations;
var blockingCollection = new BlockingCollection<int>();
Task.Factory.StartNew(() =>
{
int i = 0;
while (iterations-- > 0)
{
sw.Restart();
blockingCollection.Add(i++);
}
});
Task.Factory.StartNew(() =>
{
var expected_value = 0;
while (iterations > 0) // stop when performed certain number
{
int i = blockingCollection.Take();
sw.Stop();
long microseconds = sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));
total_time_ms += microseconds;
if (i != expected_value)
Console.WriteLine(String.Format("** expected {0} got {1}", i, expected_value));
expected_value++;
}
});
while (iterations > 0)
{
Thread.Sleep(1000);
}
Console.WriteLine(String.Format("Total {0} for {1} iterations, average {2}", total_time_ms, num_iterations, total_time_ms / num_iterations));
I believe that BlockingCollection is a good collection to use for this. There are other ways of doing it but this is a complicated area and the chances of getting something faster than this are unlikely.
Upvotes: 2
Reputation: 244837
First, if you really care about microseconds, maybe you shouldn't use .Net (because its garbage collector can give you unpredictable delays) or Windows (because it's not a real-time OS).
To actually answer your questions:
I think 50 microsecond is a really small amount of time and BlockingCollection<T>
is highly optimized, so I don't think you will be able to do it much faster. You might be able to do it using spinlocks, at the expense of wasting CPU time doing nothing.
Calling delegates asynchronously is almost the same as using ThreadPool.QueueUserWorkItem()
. So, yeah, this is probably close to the best of what you can get. Although synchronous invocations is always going to be faster.
Upvotes: 0