-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathIndexerSetReverse.cs
71 lines (61 loc) · 2.28 KB
/
IndexerSetReverse.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;
namespace System.Collections
{
[BenchmarkCategory(Categories.Runtime, Categories.Collections, Categories.GenericCollections)] // this benchmark does not belong to CoreFX because it's more a codegen benchmark
[GenericTypeArguments(typeof(int))] // value type
[GenericTypeArguments(typeof(string))] // reference type
public class IndexerSetReverse<T>
{
[Params(Utils.DefaultCollectionSize)]
public int Size;
private T[] _array;
private List<T> _list;
[GlobalSetup(Targets = new[] { nameof(Array), nameof(Span) })]
public void Setup() => _array = ValuesGenerator.ArrayOfUniqueValues<T>(Size);
[Benchmark]
public T[] Array()
{
var array = _array;
for (int i = array.Length; --i >= 0;)
array[i] = default;
return array;
}
[BenchmarkCategory(Categories.Span)]
[Benchmark]
public T Span()
{
T result = default;
var span = new Span<T>(_array);
for (int i = span.Length; --i >= 0;)
span[i] = default;
return result;
}
[GlobalSetup(Targets = new[] { nameof(List), nameof(IList) })]
public void SetupList() => _list = new List<T>(ValuesGenerator.ArrayOfUniqueValues<T>(Size));
[Benchmark]
public List<T> List()
{
var list = _list;
for (int i = list.Count; --i >= 0;)
list[i] = default;
return list;
}
[Benchmark]
[BenchmarkCategory(Categories.Runtime, Categories.Virtual)]
public IList<T> IList() => Set(_list);
[MethodImpl(MethodImplOptions.NoInlining)]
private IList<T> Set(IList<T> collection)
{
for (int i = collection.Count; --i >= 0;)
collection[i] = default;
return collection;
}
}
}