Skip to content

Instantly share code, notes, and snippets.

@sonnemaf
Created July 1, 2020 10:04
Show Gist options
  • Save sonnemaf/4b6bbba3d4fb1324f299a5ee78853b1e to your computer and use it in GitHub Desktop.
Save sonnemaf/4b6bbba3d4fb1324f299a5ee78853b1e to your computer and use it in GitHub Desktop.
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Microsoft.Toolkit.HighPerformance.Helpers;
using System;
using System.Threading.Tasks;
namespace ToolkitHighPerformanceBM {
class Program {
static void Main(string[] args) {
BenchmarkRunner.Run<BM>();
}
}
[MemoryDiagnoser]
public class BM {
private double[] _array;
[Params(10_000, 100_000, 1_000_000)]
public int Size;
[GlobalSetup]
public void Setup() => _array = new double[Size];
[Benchmark]
public void SingleThreaded() {
for (int i = 0; i < _array.Length; i++) _array[i] += 2;
}
[Benchmark]
public void Parallel_For() => Parallel.For(0, _array.Length, i => _array[i] += 2);
[Benchmark(Baseline = true)]
public void ParallelHelper_ForEach() => ParallelHelper.ForEach<double, AddTwo>(_array);
private readonly struct AddTwo : IRefAction<double> {
public void Invoke(ref double x) => x += 2;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment