Files
EightBitNet/EightBit.Benchmark/BenchmarkClockedChip.cs
T
Adrian Conlon 9bfd65fbb1 * Update nuget packages
* Namespace z80 a little better
* Add some benchmarks
2026-10-24 21:41:12 +01:00

75 lines
2.1 KiB
C#

namespace EightBit.Benchmark
{
using BenchmarkDotNet.Attributes;
class ClockingChip : ClockedChip
{
public void Reset() => this.ResetCycles();
public void Bump() => this.Cycles++;
}
public class BenchmarkClockedChip
{
private readonly ClockingChip _contents = new();
////////////////////////////////////////////
[IterationSetup]
public void IterationSetup() => this._contents.Reset();
[IterationCleanup]
public void IterationCleanup() => this._contents.Reset();
////////////////////////////////////////////
[GlobalSetup(Target = nameof(TickWithSubscribers))]
public void GlobalSetupSingle() => this._contents.Ticked += this.Contents_Ticked;
[GlobalCleanup(Target = nameof(TickWithSubscribers))]
public void GlobalCleanupSingle() => this._contents.Ticked -= this.Contents_Ticked;
[Benchmark]
public int TickWithSubscribers()
{
this._contents.Tick();
return this._contents.Cycles;
}
////////////////////////////////////////////
[GlobalSetup(Target = nameof(TickMultipleWithSubscribers))]
public void GlobalSetupMultiple() => this._contents.Ticked += this.Contents_Ticked;
[GlobalCleanup(Target = nameof(TickMultipleWithSubscribers))]
public void GlobalCleanupMultiple() => this._contents.Ticked -= this.Contents_Ticked;
[Benchmark]
public int TickMultipleWithSubscribers()
{
this._contents.Tick(4);
return this._contents.Cycles;
}
////////////////////////////////////////////
[Benchmark]
public int TickWithoutSubscribers()
{
this._contents.Tick();
return this._contents.Cycles;
}
[Benchmark]
public int TickMultipleWithoutSubscribers()
{
this._contents.Tick(4);
return this._contents.Cycles;
}
private void Contents_Ticked(object? sender, EventArgs e)
{
this._contents.Bump();
}
}
}