Last active
October 3, 2019 00:58
-
-
Save agocke/af43ef5dc1aa720f40a3939b83c4f414 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using BenchmarkDotNet.Attributes; | |
namespace misc_bench | |
{ | |
[MemoryDiagnoser] | |
public class Benchmarks | |
{ | |
private readonly int[] _items = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; | |
[Benchmark] | |
public List<int> LinqSelectWhereToList() | |
{ | |
return _items.Where(x => x % 2 == 0).Select(x => x * 2).ToList(); | |
} | |
[Benchmark] | |
public List<int> NewLinqSelectWhereToList() | |
{ | |
return new MyList(_items).Where(x => x % 2 == 0).Select(x => x * 2).ToList(); | |
} | |
[Benchmark] | |
public List<int> SelectWhereToListForEach() | |
{ | |
var list = new List<int>(); | |
foreach (var item in _items) | |
{ | |
if (item % 2 == 0) | |
{ | |
list.Add(item * 2); | |
} | |
} | |
return list; | |
} | |
} | |
} |
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
| Method | Mean | Error | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated | | |
|------------------------- |----------:|---------:|---------:|-------:|------:|------:|----------:| | |
| LinqSelectWhereToList | 141.81 ns | 2.914 ns | 3.989 ns | 0.0553 | - | - | 232 B | | |
| NewLinqSelectWhereToList | 117.59 ns | 2.333 ns | 2.777 ns | 0.0478 | - | - | 200 B | | |
| SelectWhereToListForEach | 61.84 ns | 1.339 ns | 1.963 ns | 0.0305 | - | - | 128 B | |
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; | |
using System.Collections.Generic; | |
namespace misc_bench | |
{ | |
public interface IFastEnumerable<T> | |
{ | |
IFastEnumerator<T> GetEnumerator(); | |
IFastEnumerable<T> Where(Func<T, bool> predicate) => throw null; | |
IFastEnumerable<TResult> Select<TResult>(Func<T, TResult> map) => throw null; | |
List<T> ToList() => throw null; | |
} | |
public interface IFastEnumerator<T> | |
{ | |
T Current { get; } | |
bool MoveNext(); | |
} | |
public readonly struct MyList : IFastEnumerable<int> | |
{ | |
private readonly int[] _arr; | |
public MyList(int[] arr) | |
{ | |
_arr = arr; | |
} | |
public IFastEnumerator<int> GetEnumerator() | |
{ | |
throw new System.NotImplementedException(); | |
} | |
public IFastEnumerable<int> Where(Func<int, bool> predicate) | |
{ | |
return new WhereEnumerable(_arr, predicate); | |
} | |
private struct WhereEnumerable : IFastEnumerable<int> | |
{ | |
private readonly int[] _arr; | |
private readonly Func<int, bool> _pred; | |
public WhereEnumerable(int[] arr, Func<int, bool> pred) | |
{ | |
_arr = arr; | |
_pred = pred; | |
} | |
public IFastEnumerator<int> GetEnumerator() | |
{ | |
throw new NotImplementedException(); | |
} | |
IFastEnumerable<TResult> IFastEnumerable<int>.Select<TResult>(Func<int, TResult> map) | |
{ | |
return new WhereSelectEnumerable<TResult>(_arr, _pred, map); | |
} | |
private struct WhereSelectEnumerable<TResult> : IFastEnumerable<TResult> | |
{ | |
private readonly int[] _arr; | |
private readonly Func<int, bool> _pred; | |
private readonly Func<int, TResult> _map; | |
public WhereSelectEnumerable(int[] arr, Func<int, bool> pred, Func<int, TResult> map) | |
{ | |
_arr = arr; | |
_pred = pred; | |
_map = map; | |
} | |
public IFastEnumerator<TResult> GetEnumerator() | |
{ | |
throw new NotImplementedException(); | |
} | |
public List<TResult> ToList() | |
{ | |
var list = new List<TResult>(); | |
foreach (var item in _arr) | |
{ | |
if (_pred(item)) | |
{ | |
list.Add(_map(item)); | |
} | |
} | |
return list; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment