-
Notifications
You must be signed in to change notification settings - Fork 272
/
Perf.Random.cs
83 lines (61 loc) · 2.47 KB
/
Perf.Random.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
75
76
77
78
79
80
81
82
83
// 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;
namespace System.Tests
{
[BenchmarkCategory(Categories.Libraries)]
public class Perf_Random
{
private Random _randomUnseeded = new Random();
private Random _random = new Random(123456);
private byte[] _bytes = new byte[1000];
[Benchmark]
public Random ctor_seeded() => new Random(123456);
// Retaining historical name instead of naming 'ctor' and 'ctor_unseeded'
[Benchmark]
public Random ctor() => new Random();
[Benchmark]
public int Next() => _random.Next();
[Benchmark]
[MemoryRandomization]
public int Next_unseeded() => _randomUnseeded.Next();
[Benchmark]
public int Next_int() => _random.Next(10000);
[Benchmark]
public int Next_int_unseeded() => _randomUnseeded.Next(10000);
[Benchmark]
public int Next_int_int() => _random.Next(100, 10000);
[Benchmark]
public int Next_int_int_unseeded() => _randomUnseeded.Next(100, 10000);
[Benchmark]
public void NextBytes() => _random.NextBytes(_bytes);
[Benchmark]
public void NextBytes_unseeded() => _randomUnseeded.NextBytes(_bytes);
[Benchmark]
public double NextDouble() => _random.NextDouble();
[Benchmark]
public double NextDouble_unseeded() => _randomUnseeded.NextDouble();
#if !NETFRAMEWORK // New API in .NET Core 2.1
[Benchmark]
public void NextBytes_span() => _random.NextBytes(_bytes.AsSpan());
[Benchmark]
public void NextBytes_span_unseeded() => _randomUnseeded.NextBytes(_bytes.AsSpan());
#endif
#if NET6_0_OR_GREATER // New API in .NET 6.0
[Benchmark]
public long Next_long() => _random.NextInt64(2^20);
[Benchmark]
public long Next_long_unseeded() => _randomUnseeded.NextInt64(2^48);
[Benchmark]
public long Next_long_long() => _random.NextInt64(100, 10000);
[Benchmark]
public long Next_long_long_unseeded() => _randomUnseeded.NextInt64(100, 10000);
[Benchmark]
public float NextSingle() => _random.NextSingle();
[Benchmark]
public float NextSingle_unseeded() => _randomUnseeded.NextSingle();
#endif
}
}