Created
August 31, 2016 01:15
-
-
Save jackmott/50dec43b93d206a1f758206b7b9be37e to your computer and use it in GitHub Desktop.
LinqTestSource
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> ListT; | |
public LinkedList<int> linkedList; | |
[Params(100000)] | |
public int length { get; set; } | |
[Setup] | |
public void setupData() | |
{ | |
rawArray = new int[this.length]; | |
ListT = new List<int>(); | |
linkedList = new LinkedList<int>(); | |
for (int i = 0; i < this.length; i++) | |
{ | |
rawArray[i] = i; | |
ListT.Add(i); | |
linkedList.AddFirst(i); | |
} | |
} | |
[Benchmark] | |
public int OneWhereLinkedList() | |
{ | |
var linkedListLocal = linkedList; | |
return linkedListLocal.Where(x => x < 50000 && x % 2 == 0).Sum(); | |
} | |
[Benchmark] | |
public int TwoWhereLinkedList() | |
{ | |
var linkedListLocal = linkedList; | |
return linkedListLocal.Where(x => x < 50000).Where(x => x % 2 == 0).Sum(); | |
} | |
[Benchmark] | |
public int OneWhereList() | |
{ | |
var listLocal = ListT; | |
return listLocal.Where(x => x < 50000 && x % 2 == 0).Sum(); | |
} | |
[Benchmark] | |
public int TwoWhereList() | |
{ | |
var listLocal = ListT; | |
return listLocal.Where(x => x < 50000).Where(x => x % 2 == 0).Sum(); | |
} | |
[Benchmark] | |
public int OneWhereArray() | |
{ | |
var rawArrayLocal = rawArray; | |
return rawArrayLocal.Where(x => x < 50000 && x % 2 == 0).Sum(); | |
} | |
[Benchmark] | |
public int TwoWhereArray() | |
{ | |
var rawArrayLocal = rawArray; | |
return rawArrayLocal.Where(x => x < 50000).Where(x => x % 2 == 0).Sum(); | |
} | |
[Benchmark] | |
public int NoLinq() | |
{ | |
var listLocal = ListT; | |
//Can optionally pre-size the List if you have an idea how many results there wil be | |
int sum = 0; | |
for (int i = 0; i < listLocal.Count; i++) | |
{ | |
var x = listLocal[i]; | |
if (x < 50000 && x % 2 == 0) sum += x; | |
} | |
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