|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using BenchmarkDotNet.Attributes; |
| 6 | +using Light.GuardClauses.FrameworkExtensions; |
| 7 | + |
| 8 | +namespace Light.GuardClauses.Performance.CollectionAssertions; |
| 9 | + |
| 10 | +public class ListCountBenchmark |
| 11 | +{ |
| 12 | + public List<int> List; |
| 13 | + |
| 14 | + // ReSharper disable once UnusedAutoPropertyAccessor.Global |
| 15 | + [Params(100, 1000)] |
| 16 | + public int NumberOfItems { get; set; } |
| 17 | + |
| 18 | + [GlobalSetup] |
| 19 | + public void Setup() => List = Enumerable.Range(1, NumberOfItems).ToList(); |
| 20 | + |
| 21 | + // ReSharper disable once UseCollectionCountProperty |
| 22 | + [Benchmark(Baseline = true)] |
| 23 | + public int LinqCount() => List.Count(); |
| 24 | + |
| 25 | + [Benchmark] |
| 26 | + public int ExistingEnumerableCount() => EnumerableExtensions.Count(List); |
| 27 | + |
| 28 | + [Benchmark] |
| 29 | + public int NewEnumerableOfTGetCount() => List.GetCount(); |
| 30 | +} |
| 31 | + |
| 32 | +public class StringCountBenchmark |
| 33 | +{ |
| 34 | + public string String; |
| 35 | + |
| 36 | + // ReSharper disable once UnusedAutoPropertyAccessor.Global |
| 37 | + [Params(100, 1000)] |
| 38 | + public int NumberOfItems { get; set; } |
| 39 | + |
| 40 | + [GlobalSetup] |
| 41 | + public void Setup() |
| 42 | + { |
| 43 | + var random = new Random(42); |
| 44 | + var array = new char[NumberOfItems]; |
| 45 | + for (var i = 0; i < array.Length; i++) |
| 46 | + { |
| 47 | + array[i] = (char) random.Next('a', 'z' + 1); |
| 48 | + } |
| 49 | + |
| 50 | + String = new (array); |
| 51 | + } |
| 52 | + |
| 53 | + // ReSharper disable once UseCollectionCountProperty |
| 54 | + [Benchmark(Baseline = true)] |
| 55 | + public int LinqCount() => String.Count(); |
| 56 | + |
| 57 | + [Benchmark] |
| 58 | + public int ExistingEnumerableCount() => EnumerableExtensions.Count(String); |
| 59 | + |
| 60 | + [Benchmark] |
| 61 | + public int NewEnumerableOfTGetCount() => String.GetCount(); |
| 62 | +} |
| 63 | + |
| 64 | +public class MyImmutableArrayCountBenchmark |
| 65 | +{ |
| 66 | + public MyImmutableArray<int> MyArray; |
| 67 | + |
| 68 | + // ReSharper disable once UnusedAutoPropertyAccessor.Global |
| 69 | + [Params(100, 1000)] |
| 70 | + public int NumberOfItems { get; set; } |
| 71 | + |
| 72 | + [GlobalSetup] |
| 73 | + public void Setup() => MyArray = new (Enumerable.Range(1, NumberOfItems).ToArray()); |
| 74 | + |
| 75 | + // ReSharper disable once UseCollectionCountProperty |
| 76 | + [Benchmark(Baseline = true)] |
| 77 | + public int LinqCount() => MyArray.Count(); |
| 78 | + |
| 79 | + [Benchmark] |
| 80 | + public int ExistingEnumerableCount() => EnumerableExtensions.Count(MyArray); |
| 81 | + |
| 82 | + [Benchmark] |
| 83 | + public int NewEnumerableOfTGetCount() => MyArray.GetCount(); |
| 84 | +} |
| 85 | + |
| 86 | +public sealed class MyImmutableArray<T> : IReadOnlyList<T> |
| 87 | +{ |
| 88 | + private readonly T[] _array; |
| 89 | + |
| 90 | + public MyImmutableArray(T[] array) => _array = array; |
| 91 | + |
| 92 | + public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) _array).GetEnumerator(); |
| 93 | + |
| 94 | + IEnumerator IEnumerable.GetEnumerator() => _array.GetEnumerator(); |
| 95 | + |
| 96 | + public int Count => _array.Length; |
| 97 | + |
| 98 | + public T this[int index] => _array[index]; |
| 99 | +} |
0 commit comments