-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathCtorFromCollectionNonGeneric.cs
43 lines (33 loc) · 1.6 KB
/
CtorFromCollectionNonGeneric.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
// 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 BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;
namespace System.Collections
{
[BenchmarkCategory(Categories.Libraries, Categories.Collections, Categories.NonGenericCollections)]
[GenericTypeArguments(typeof(int))] // value type (it shows how bad idea is to use non-generic collections for value types)
[GenericTypeArguments(typeof(string))] // reference type
public class CtorFromCollectionNonGeneric<T>
{
private ICollection _collection;
private IDictionary _dictionary;
[Params(Utils.DefaultCollectionSize)]
public int Size;
[GlobalSetup(Targets = new[] { nameof(ArrayList), nameof(Queue), nameof(Stack) })]
public void SetupCollection() => _collection = ValuesGenerator.ArrayOfUniqueValues<T>(Size);
[GlobalSetup(Targets = new[] { nameof(Hashtable), nameof(SortedList) })]
public void SetupDictionary() => _dictionary = ValuesGenerator.Dictionary<T, T>(Size);
[Benchmark]
public ArrayList ArrayList() => new ArrayList(_collection);
[Benchmark]
public Hashtable Hashtable() => new Hashtable(_dictionary);
[Benchmark]
public Queue Queue() => new Queue(_collection);
[Benchmark]
public Stack Stack() => new Stack(_collection);
[Benchmark]
public SortedList SortedList() => new SortedList(_dictionary);
}
}