-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumSectorsBenchmarks.cs
More file actions
64 lines (57 loc) · 1.99 KB
/
Copy pathNumSectorsBenchmarks.cs
File metadata and controls
64 lines (57 loc) · 1.99 KB
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
using BenchmarkDotNet.Attributes;
namespace XISOSharp.Benchmarks;
[MemoryDiagnoser]
[MinIterationCount(5)]
[MaxIterationCount(20)]
#pragma warning disable RCS1102
// ReSharper disable once ClassNeverInstantiated.Global
// ReSharper disable once ConvertToStaticClass
public class NumSectorsBenchmarks
#pragma warning restore RCS1102
{
/// <summary>
/// Gets or sets the input byte count. Instance state (not a compile-time
/// constant) so the JIT cannot constant-fold the call away (BUG-BEN-004).
/// </summary>
[Params(1u, 2048u, 204800u, 204801u, uint.MaxValue)]
public uint ByteCount { get; set; }
private uint[] _batch = null!;
/// <summary>
/// Builds a batch of varying inputs around <see cref="ByteCount"/> so the
/// loop benchmark measures real arithmetic, not one folded constant.
/// </summary>
[GlobalSetup]
public void Setup()
{
_batch = new uint[256];
for (int i = 0; i < _batch.Length; i++)
{
_batch[i] = unchecked(ByteCount + (uint)i);
}
}
/// <summary>
/// Measures sector rounding for the parameterized input.
/// Instance field input defeats constant folding; the returned value
/// defeats dead-code elimination.
/// </summary>
/// <returns>The sector count.</returns>
[Benchmark(Baseline = true)]
public uint NumSectors_Param() => Constants.NumSectors(ByteCount);
/// <summary>
/// Measures sector rounding over a 256-entry batch with varying inputs and
/// accumulates the sum, lifting the single-op cost above timer resolution
/// (BUG-BEN-004: one integer op is pure call overhead otherwise).
/// </summary>
/// <returns>Sum of sector counts (prevents folding/elimination).</returns>
[Benchmark]
public uint NumSectors_Batch256()
{
uint sum = 0u;
uint[] batch = _batch;
for (int i = 0; i < batch.Length; i++)
{
sum += Constants.NumSectors(batch[i]);
}
return sum;
}
}