-
Notifications
You must be signed in to change notification settings - Fork 272
/
Perf.Int32.cs
74 lines (61 loc) · 2.51 KB
/
Perf.Int32.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
72
73
74
// 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.Globalization;
using System.Linq;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace System.Tests
{
[BenchmarkCategory(Categories.Libraries)]
public class Perf_Int32
{
private char[] _destination = new char[int.MinValue.ToString().Length];
public static IEnumerable<object> Values => new object[]
{
int.MinValue,
4, // single digit
(int)12345, // same value used by other tests to compare the perf
int.MaxValue
};
public static IEnumerable<object> StringValuesDecimal => Values.Select(value => value.ToString()).ToArray();
public static IEnumerable<object> StringValuesHex => Values.Select(value => ((int)value).ToString("X")).ToArray();
[Benchmark]
[ArgumentsSource(nameof(Values))]
public string ToString(int value) => value.ToString();
[Benchmark]
[ArgumentsSource(nameof(Values))]
[MemoryRandomization]
public string ToStringHex(int value) => value.ToString("X");
[Benchmark]
[ArgumentsSource(nameof(StringValuesDecimal))]
[MemoryRandomization]
public int Parse(string value) => int.Parse(value);
[Benchmark]
[ArgumentsSource(nameof(StringValuesHex))]
public int ParseHex(string value) => int.Parse(value, NumberStyles.HexNumber);
[Benchmark]
[ArgumentsSource(nameof(StringValuesDecimal))]
[MemoryRandomization]
public bool TryParse(string value) => int.TryParse(value, out _);
#if !NETFRAMEWORK // API added in .NET Core 2.1
[Benchmark]
[ArgumentsSource(nameof(StringValuesDecimal))]
public int ParseSpan(string value) => int.Parse(value.AsSpan());
[Benchmark]
[ArgumentsSource(nameof(Values))]
[MemoryRandomization]
public bool TryFormat(int value) => value.TryFormat(new Span<char>(_destination), out _);
[Benchmark]
[ArgumentsSource(nameof(StringValuesDecimal))]
[MemoryRandomization]
public bool TryParseSpan(string value) => int.TryParse(value.AsSpan(), out _);
#endif
#if NET7_0_OR_GREATER
[Benchmark]
[Arguments(1, -1)]
public int CopySign(int value, int sign) => int.CopySign(value, sign);
#endif
}
}