Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using BenchmarkDotNet.Attributes;
- using BenchmarkDotNet.Configs;
- using BenchmarkDotNet.Diagnostics.Windows;
- using BenchmarkDotNet.Jobs;
- using BenchmarkDotNet.Running;
- namespace ConsoleApplication27
- {
- class Program
- {
- static void Main()
- {
- BenchmarkRunner.Run<Test>(ManualConfig
- .Create(DefaultConfig.Instance)
- .With(new MemoryDiagnoser()));
- Console.WriteLine(Test.MyCount);
- }
- }
- public class Test
- {
- public static int MyCount { get; private set; }
- [Benchmark]
- public Task Bag()
- {
- var bag = new ConcurrentBag<int>();
- return Run(bag, i => bag.Add(i));
- }
- [Benchmark]
- public Task Queue()
- {
- var queue = new ConcurrentQueue<int>();
- return Run(queue, i => queue.Enqueue(i));
- }
- private async Task Run(IEnumerable<int> c, Action<int> add)
- {
- await Task.WhenAll(Enumerable.Range(0, Environment.ProcessorCount).Select(x => Task.Run(() =>
- {
- for (int i = 0; i < 100000; i++)
- {
- add(i);
- }
- })));
- foreach (var i in c)
- {
- MyCount++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement