I am benchmarking the performance of the BlockingCollection
.
class Program
{
private static BlockingCollection<int> bc = new BlockingCollection<int>(100000);
private static BlockingCollection<int> bc2 = new BlockingCollection<int>(100000);
static void Main(string[] args)
{
for (int i = 0; i < 100000; i++)
{
bc.TryAdd(i);
}
var stopWatch = new Stopwatch();
stopWatch.Start();
while (bc.Count != 0)
{
bc.Take();
}
stopWatch.Stop();
Console.WriteLine("Simple " + stopWatch.Elapsed.TotalMilliseconds);
createThreadPool(bc2);
Console.Read();
}
static void ByTaskRun(BlockingCollection<int> blockingCollection)
{
for (int i = 0; i < 100000; i++)
{
blockingCollection.TryAdd(i);
}
var stopWatch = new Stopwatch();
stopWatch.Start();
Task k = Task.Run(() =>
{
while (blockingCollection.Count != 0)
{
blockingCollection.Take();
}
});
stopWatch.Stop();
Console.WriteLine("Task.run " + stopWatch.Elapsed.TotalMilliseconds);
}
}
In the main()
method, I simply take the elements from a blocking collection while in ByTaskRun()
I run a task to take the elements out. I found that Task.Run()
is a faster one. Does it create a Threadpool
internally? If I want to to get the number of threads created by the Task.Run
, how can I get that number?
In this sample Task.Run()
use only one thread. And you get not actual working duration.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.