-
Notifications
You must be signed in to change notification settings - Fork 272
/
Perf_Dictionary.cs
46 lines (38 loc) · 1.17 KB
/
Perf_Dictionary.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
// 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.Linq;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace System.Collections.Tests
{
[BenchmarkCategory(Categories.Libraries, Categories.Collections, Categories.GenericCollections)]
public class Perf_Dictionary
{
[Params(3_000)]
public int Items;
private Dictionary<int, int> _dict;
[GlobalSetup]
public void InitializeContainsValue()
{
_dict = Enumerable.Range(0, 3_000).ToDictionary(i => i);
}
[Benchmark]
public object Clone() => new Dictionary<int, int>(_dict);
[Benchmark]
public int ContainsValue()
{
Dictionary<int, int> d = _dict;
int count = 0;
for (int i = 0; i < Items; i++)
{
if (d.ContainsValue(i))
{
count++;
}
}
return count;
}
}
}