// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. using System.Buffers; using BenchmarkDotNet.Attributes; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; // ReSharper disable InconsistentNaming namespace SixLabors.ImageSharp.Benchmarks.Bulk; public abstract class FromRgba32Bytes where TPixel : unmanaged, IPixel { private IMemoryOwner destination; private IMemoryOwner source; private Configuration configuration; [Params( 128, 1024, 2048)] public int Count { get; set; } [GlobalSetup] public void Setup() { this.configuration = Configuration.Default; this.destination = this.configuration.MemoryAllocator.Allocate(this.Count); this.source = this.configuration.MemoryAllocator.Allocate(this.Count * 4); } [GlobalCleanup] public void Cleanup() { this.destination.Dispose(); this.source.Dispose(); } // [Benchmark] public void Naive() { Span s = this.source.GetSpan(); Span d = this.destination.GetSpan(); for (int i = 0; i < this.Count; i++) { int i4 = i * 4; d[i] = TPixel.FromRgba32(new Rgba32(s[i4], s[i4 + 1], s[i4 + 2], s[i4 + 3])); } } [Benchmark(Baseline = true)] public void CommonBulk() => new PixelOperations().FromRgba32Bytes(this.configuration, this.source.GetSpan(), this.destination.GetSpan(), this.Count); [Benchmark] public void OptimizedBulk() => PixelOperations.Instance.FromRgba32Bytes(this.configuration, this.source.GetSpan(), this.destination.GetSpan(), this.Count); } public class FromRgba32Bytes_ToRgba32 : FromRgba32Bytes; public class FromRgba32Bytes_ToBgra32 : FromRgba32Bytes { // RESULTS: // Method | Count | Mean | Error | StdDev | Scaled | // -------------- |------ |-----------:|----------:|----------:|-------:| // CommonBulk | 128 | 207.1 ns | 3.723 ns | 3.300 ns | 1.00 | // OptimizedBulk | 128 | 166.5 ns | 1.204 ns | 1.005 ns | 0.80 | // | | | | | | // CommonBulk | 1024 | 1,333.9 ns | 12.426 ns | 11.624 ns | 1.00 | // OptimizedBulk | 1024 | 974.1 ns | 18.803 ns | 16.669 ns | 0.73 | // | | | | | | // CommonBulk | 2048 | 2,625.4 ns | 30.143 ns | 26.721 ns | 1.00 | // OptimizedBulk | 2048 | 1,843.0 ns | 20.505 ns | 18.177 ns | 0.70 | }