Files
Misaki.HighPerformance/Misaki.HighPerformance.Test/Benchmark/ParallelNoiseBenchmark.cs
Misaki 37d548085e Refactor job API: add JobExecutionContext, update tests
Major breaking change: job interfaces now use JobExecutionContext
instead of threadIndex, enabling thread-aware and dynamic job
dispatching. Updated all job system, SPMD, and test code to match.
Collections improved with new methods and clearer enumerators.
Renamed IJobScheduler.WaitComplete to Wait. Incremented project
versions. Includes bug fixes, documentation, and style updates.
2026-03-04 11:43:39 +09:00

71 lines
1.8 KiB
C#

using BenchmarkDotNet.Attributes;
using Misaki.HighPerformance.Jobs;
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections;
using Misaki.HighPerformance.Test.Jobs;
using System.Numerics;
namespace Misaki.HighPerformance.Test.Benchmark;
[MemoryDiagnoser]
public class ParallelNoiseBenchmark
{
private const int _WIDTH = 2048;
private const int _HEIGHT = 2048;
private const int _LENGTH = _WIDTH * _HEIGHT;
internal JobScheduler _jobScheduler = null!;
private UnsafeArray<float> _buffers;
[GlobalSetup]
public void Setup()
{
_jobScheduler = new JobScheduler(Environment.ProcessorCount);
_buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent);
}
[GlobalCleanup]
public void Cleanup()
{
_jobScheduler.Dispose();
_buffers.Dispose();
}
[Benchmark]
public unsafe void JobSystem()
{
var job = new NoiseJobVector()
{
buffers = (float*)_buffers.GetUnsafePtr(),
width = _WIDTH,
height = _HEIGHT
};
var handle = _jobScheduler.ScheduleParallel(ref job, _LENGTH, 64, -1, JobHandle.Invalid);
_jobScheduler.Wait(handle);
}
[Benchmark]
public void ParallelFor()
{
Parallel.For(0, _LENGTH, i =>
{
var x = i % _WIDTH;
var y = i / _HEIGHT;
var uv = new Vector2(x, y) / new Vector2(_WIDTH, _HEIGHT);
_buffers[i] = NoiseJobVector.GradientNoise(uv);
});
}
[Benchmark(Baseline = true)]
public void For()
{
for (var i = 0; i < _LENGTH; i++)
{
var x = i % _WIDTH;
var y = i / _HEIGHT;
var uv = new Vector2(x, y) / new Vector2(_WIDTH, _HEIGHT);
_buffers[i] = NoiseJobVector.GradientNoise(uv);
}
}
}