-
-
Save mattwarren/8190ab7e4b139764d686d9bb279711b2 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
public enum TestMode | |
{ | |
List, | |
IEnumerable, | |
Null, | |
} | |
// See https://twitter.com/Nick_Craver/status/724331063414235137 | |
[Config(typeof(Config))] | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run<Program>(); | |
} | |
private class Config : ManualConfig | |
{ | |
public Config() | |
{ | |
Add(Job.Clr.WithLaunchCount(1)); | |
Add(new GCDiagnoser()); | |
Add(RPlotExporter.Default); | |
Add(CsvMeasurementsExporter.Default); // Without this RPlot Exporter doesn't work!!! | |
} | |
} | |
private List<int> list = Enumerable.Range(0, 100).Select(i => i).ToList(); | |
private IEnumerable<int> enumerable = Enumerable.Range(0, 100).Select(i => i); | |
private IEnumerable<int> listToUse; | |
[Params(TestMode.List, TestMode.IEnumerable, TestMode.Null)] | |
public TestMode Mode { get; set; } | |
[Setup] | |
public void Setup() | |
{ | |
switch (Mode) | |
{ | |
case TestMode.List: | |
listToUse = list; | |
break; | |
case TestMode.IEnumerable: | |
listToUse = enumerable; | |
break; | |
case TestMode.Null: | |
listToUse = null; | |
break; | |
default: | |
throw new InvalidOperationException("Something went wrong!!"); | |
} | |
} | |
[Benchmark] | |
public List<int> NickCraver() | |
{ | |
return listToUse.AsListNickCraver(); | |
} | |
[Benchmark] | |
public List<int> KirkWoll() | |
{ | |
return listToUse.AsListKirkWoll(); | |
} | |
} | |
public static class Extensions | |
{ | |
// See https://twitter.com/Nick_Craver/status/724320336691470337 | |
public static List<T> AsListNickCraver<T>(this IEnumerable<T> source) => | |
source != null && !(source is List<T>) ? source.ToList() : (List<T>)source; | |
// See https://twitter.com/kirkwoll/status/724325018662903808 | |
public static List<T> AsListKirkWoll<T>(this IEnumerable<T> source) => | |
source as List<T> ?? source?.ToList(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment