Last active
May 25, 2018 15:27
-
-
Save adamsitnik/fe00ae5f6a4d324aa85d4952aaaab8f6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace ConcreteVsAbstract | |
{ | |
class Program | |
{ | |
static void Main(string[] args) => BenchmarkRunner.Run<Benchmarks>(); | |
} | |
[MemoryDiagnoser] | |
public class Benchmarks | |
{ | |
[Params(2, 10, 100, 1000, 10000)] | |
public int Size; | |
private int[] array; | |
[GlobalSetup] | |
public void Setup() => array = Enumerable.Range(0, Size).ToArray(); | |
[Benchmark] | |
public int IterateOverConcreteArray() | |
{ | |
int[] concrete = array; | |
int result = 0; | |
for (int i = 0; i < concrete.Length; i++) | |
result += concrete[i]; | |
return result; | |
} | |
[Benchmark] | |
public int IterateOverArrayAsIReadonlyList() | |
{ | |
IReadOnlyList<int> abstraction = array; | |
int result = 0; | |
for (int i = 0; i < abstraction.Count; i++) | |
result += abstraction[i]; | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment