Created
February 5, 2021 09:23
-
-
Save kevingosse/3ac1979ae835a46d10ae04f56d9638fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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