Skip to content

Instantly share code, notes, and snippets.

@kevingosse
Created February 5, 2021 09:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevingosse/3ac1979ae835a46d10ae04f56d9638fe to your computer and use it in GitHub Desktop.
Save kevingosse/3ac1979ae835a46d10ae04f56d9638fe to your computer and use it in GitHub Desktop.
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace BenchmarkCore
{
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<ArrayGet>();
}
}
public class ArrayGet
{
public int[] ArrayInt;
public StrongBox<int>[] ArrayObject;
[Params(100, 1000, 10_000)]
public int N;
[GlobalSetup]
public void Setup()
{
ArrayObject = new StrongBox<int>[N];
for (int i = 0; i < N; i++) ArrayObject[i] = new StrongBox<int>(i);
ArrayInt = new int[N];
for (int i = 0; i < N; i++) ArrayInt[i] = i;
}
[Benchmark(Baseline = true)]
public int Sum()
{
int sum = 0;
for (int i = 0; i < N; i++)
sum += ArrayInt[i];
return sum;
}
[Benchmark]
public int SumObject()
{
int sum = 0;
for (int i = 0; i < N; i++)
sum += ArrayObject[i].Value;
return sum;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment