-
Notifications
You must be signed in to change notification settings - Fork 272
/
Perf.Decimal.cs
61 lines (47 loc) · 1.67 KB
/
Perf.Decimal.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
// 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 MicroBenchmarks;
using System.Collections.Generic;
using System.Linq;
namespace System.Tests
{
[BenchmarkCategory(Categories.Libraries)]
public class Perf_Decimal
{
public static IEnumerable<object> Values => new object[]
{
new decimal(1.23456789E+5)
};
public static IEnumerable<object> StringValues
=> Values.OfType<decimal>().Select(value => value.ToString());
[Benchmark]
[ArgumentsSource(nameof(Values))]
[MemoryRandomization]
public string ToString(decimal value) => value.ToString();
[Benchmark]
[ArgumentsSource(nameof(StringValues))]
public decimal Parse(string value) => decimal.Parse(value);
[Benchmark]
[ArgumentsSource(nameof(StringValues))]
public bool TryParse(string value) => decimal.TryParse(value, out _);
private decimal _a = 67891.2345m;
private decimal _b = 12345.6789m;
[Benchmark]
public decimal Add() => _a + _b;
[Benchmark]
public decimal Subtract() => _a - _b;
[Benchmark]
public decimal Multiply() => _a * _b;
[Benchmark]
public decimal Divide() => _a / _b;
[Benchmark]
[MemoryRandomization]
public decimal Mod() => _a % _b;
[Benchmark]
public decimal Floor() => decimal.Floor(_a);
[Benchmark]
public decimal Round() => decimal.Round(_a);
}
}