|
1 | | -using System.Collections.Generic; |
2 | | -using System.Linq; |
| 1 | +using System.Linq; |
3 | 2 | using BenchmarkDotNet.Attributes; |
4 | | -using StructLinq.IEnumerable; |
5 | | -using StructLinq.Range; |
6 | | -using StructLinq.Select; |
7 | 3 |
|
8 | 4 | namespace StructLinq.Benchmark |
9 | 5 | { |
10 | | - [MemoryDiagnoser] |
| 6 | + |
| 7 | + //BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18363 |
| 8 | + //Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores |
| 9 | + //.NET Core SDK=3.1.101 |
| 10 | + //[Host] : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT |
| 11 | + //DefaultJob : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT |
| 12 | + |
| 13 | + |
| 14 | + //``` |
| 15 | + //| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated | |
| 16 | + //|--------------- |---------:|---------:|---------:|------:|--------:|------:|------:|------:|----------:| |
| 17 | + //| SysSelect | 66.87 us | 1.280 us | 1.524 us | 1.00 | 0.00 | - | - | - | 88 B | |
| 18 | + //| DelegateSelect | 30.33 us | 0.606 us | 0.744 us | 0.45 | 0.02 | - | - | - | - | |
| 19 | + //| StructSelect | 19.63 us | 0.213 us | 0.189 us | 0.30 | 0.01 | - | - | - | - | |
| 20 | + //| ConvertSelect | 69.85 us | 1.508 us | 1.411 us | 1.05 | 0.03 | - | - | - | 40 B | |
| 21 | + |
| 22 | + [MemoryDiagnoser, DisassemblyDiagnoser(recursiveDepth: 4)] |
11 | 23 | public class Select |
12 | 24 | { |
13 | | - private readonly IEnumerable<double> sysRange; |
14 | | - private readonly IStructEnumerable<double, SelectEnumerator<int, double, RangeEnumerator, StructFunction<int, double>>> delegateRange; |
15 | | - private readonly IStructEnumerable<double, SelectEnumerator<int, double, GenericEnumerator<int>, StructFunction<int, double>>> convertRange; |
16 | | - private readonly CountAction<double>[] countActions = new CountAction<double>[1]; |
17 | 25 | private const int Count = 10000; |
18 | | - private readonly IStructEnumerable<double, SelectEnumerator<int, double, RangeEnumerator, MultFunction>> structRange; |
19 | 26 | public Select() |
20 | 27 | { |
21 | | - sysRange = Enumerable.Range(0, Count).Select(x=> x * 2.0); |
22 | | - delegateRange = StructEnumerable.Range(0, Count).Select(x=> x * 2.0); |
23 | | - convertRange = Enumerable.Range(0, Count).ToStructEnumerable().Select(x=> x * 2.0); |
24 | | - var multFunction = new MultFunction(); |
25 | | - structRange = StructEnumerable.Range(0, Count).Select(in multFunction, Id<double>.Value); |
26 | 28 | } |
27 | 29 |
|
28 | 30 | [Benchmark(Baseline = true)] |
29 | | - public int SysSelect() |
| 31 | + public double SysSelect() |
30 | 32 | { |
31 | | - int count = 0; |
32 | | - foreach (var i in sysRange) |
| 33 | + double sum = 0; |
| 34 | + var enumerable = Enumerable.Range(0, Count).Select(x=> x * 2.0); |
| 35 | + foreach (var i in enumerable) |
33 | 36 | { |
34 | | - count++; |
| 37 | + sum+= i; |
35 | 38 | } |
36 | | - return count; |
| 39 | + return sum; |
37 | 40 | } |
38 | 41 |
|
39 | 42 | [Benchmark] |
40 | | - public int DelegateSelect() |
| 43 | + public double DelegateSelect() |
41 | 44 | { |
42 | | - int count = 0; |
43 | | - delegateRange.ForEach(i => count++); |
44 | | - return count; |
| 45 | + return StructEnumerable |
| 46 | + .Range(0, Count) |
| 47 | + .Select(x => x * 2.0, x => x) |
| 48 | + .Sum(x=>x); |
45 | 49 | } |
46 | 50 |
|
47 | 51 | [Benchmark] |
48 | | - public int StructSelect() |
| 52 | + public double StructSelect() |
49 | 53 | { |
50 | | - ref CountAction<double> countAction = ref countActions[0]; |
51 | | - countAction.Count = 0; |
52 | | - structRange.ForEach(ref countAction); |
53 | | - return countAction.Count; |
| 54 | + var multFunction = new MultFunction(); |
| 55 | + return StructEnumerable.Range(0, Count) |
| 56 | + .Select(ref multFunction, x=>x, x => x) |
| 57 | + .Sum(x=> x); |
54 | 58 | } |
55 | 59 |
|
56 | 60 | [Benchmark] |
57 | | - public int ConvertSelect() |
| 61 | + public double ConvertSelect() |
58 | 62 | { |
59 | | - ref CountAction<double> countAction = ref countActions[0]; |
60 | | - countAction.Count = 0; |
61 | | - convertRange.ForEach(ref countAction); |
62 | | - return countAction.Count; |
| 63 | + return Enumerable.Range(0, Count) |
| 64 | + .ToStructEnumerable() |
| 65 | + .Select(x=> x * 2.0, x => x) |
| 66 | + .Sum(x=>x); |
63 | 67 | } |
64 | 68 | } |
65 | 69 |
|
66 | | - struct MultFunction : IFunction<int, double> |
| 70 | + readonly struct MultFunction : IFunction<int, double> |
67 | 71 | { |
68 | | - public readonly double Eval(in int element) |
| 72 | + public double Eval(int element) |
69 | 73 | { |
70 | 74 | return element * 2.0; |
71 | 75 | } |
|
0 commit comments