Created
August 23, 2016 12:59
-
-
Save jackmott/52daa2c071df50d0497c0127d1880dec to your computer and use it in GitHub Desktop.
C# benchmarks
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.Collections.Generic; | |
using System.Linq; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using BenchmarkDotNet.Configs; | |
using BenchmarkDotNet.Jobs; | |
using BenchmarkDotNet.Diagnostics.Windows; | |
namespace bigo | |
{ | |
[Config(typeof(Config))] | |
public class Bench | |
{ | |
private class Config : ManualConfig | |
{ | |
public Config() | |
{ | |
Add(Job.RyuJitX64); | |
Add(new MemoryDiagnoser()); | |
} | |
} | |
public int[] rawArray; | |
public List<int> arrayList; | |
public LinkedList<int> linkedList; | |
[Params(100000)] | |
public int length { get; set; } | |
[Setup] | |
public void setupData() | |
{ | |
rawArray = new int[this.length]; | |
arrayList = new List<int>(); | |
linkedList = new LinkedList<int>(); | |
for (int i = 0; i < this.length; i++) | |
{ | |
rawArray[i] = 1; | |
arrayList.Add(1); | |
linkedList.AddFirst(1); | |
} | |
} | |
[Benchmark(Baseline = true)] | |
public int LinkedListLinq() | |
{ | |
var local = linkedList; | |
return local.Sum(); | |
} | |
[Benchmark] | |
public int LinkedListForEach() | |
{ | |
var local = linkedList; | |
int sum = 0; | |
checked | |
{ | |
foreach (var node in local) | |
{ | |
sum += node; | |
} | |
} | |
return sum; | |
} | |
[Benchmark] | |
public int LinkedListFor() | |
{ | |
var local = linkedList; | |
int sum = 0; | |
var node = local.First; | |
for (int i = 0; i < local.Count; i++) | |
{ | |
checked | |
{ | |
sum += node.Value; | |
node = node.Next; | |
} | |
} | |
return sum; | |
} | |
[Benchmark] | |
public int ArrayListFor() | |
{ | |
var local = arrayList; | |
int sum = 0; | |
for (int i = 0; i < local.Count; i++) | |
{ | |
checked | |
{ | |
sum += local[i]; | |
} | |
} | |
return sum; | |
} | |
[Benchmark] | |
public int ArrayListForEach() | |
{ | |
var local = arrayList; | |
int sum = 0; | |
checked | |
{ | |
foreach (var x in local) | |
{ | |
sum += x; | |
} | |
} | |
return sum; | |
} | |
[Benchmark] | |
public int RawArrayLinq() | |
{ | |
var local = rawArray; | |
return local.Sum(); | |
} | |
[Benchmark] | |
public int RawArrayForEach() | |
{ | |
var local = rawArray; | |
int sum = 0; | |
checked | |
{ | |
foreach (var x in local) | |
{ | |
sum += x; | |
} | |
} | |
return sum; | |
} | |
[Benchmark] | |
public int RawArrayFor() | |
{ | |
var local = rawArray; | |
int sum = 0; | |
for (int i = 0; i < local.Length; i++) | |
{ | |
checked | |
{ | |
sum += local[i]; | |
} | |
} | |
return sum; | |
} | |
static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run<Bench>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment