-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathStringValuesBenchmark.cs
63 lines (51 loc) · 1.81 KB
/
StringValuesBenchmark.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
// 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;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using MicroBenchmarks;
namespace Microsoft.Extensions.Primitives.Performance
{
[BenchmarkCategory(Categories.Libraries)]
public class StringValuesBenchmark
{
private readonly string _string = "Hello world!";
private readonly string[] _stringArray = new[] { "Hello", "world", "!" };
private readonly StringValues _stringBased = new StringValues("Hello world!");
private readonly StringValues _arrayBased = new StringValues(new[] { "Hello", "world", "!" });
[Benchmark]
public StringValues Ctor_String() => new StringValues(_string);
[Benchmark]
public StringValues Ctor_Array() => new StringValues(_stringArray);
[Benchmark]
public string Indexer_FirstElement_String() => _stringBased[0];
[Benchmark]
public string Indexer_FirstElement_Array() => _arrayBased[0];
[Benchmark]
public int Count_String() => _stringBased.Count;
[Benchmark]
public int Count_Array() => _arrayBased.Count;
[Benchmark]
public int ForEach_String()
{
int result = 0;
foreach (var item in _stringBased)
{
result += item.Length;
}
return result;
}
[Benchmark]
public int ForEach_Array()
{
int result = 0;
foreach (var item in _arrayBased)
{
result += item.Length;
}
return result;
}
}
}