Added a bunch of benchmarks

This commit is contained in:
Darth Affe 2024-07-21 21:36:26 +02:00
parent cd4ad4de78
commit cc8de89481
7 changed files with 339 additions and 14 deletions

View File

@ -0,0 +1,72 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using HPPH.Reference;
namespace HPPH.Benchmark;
[SimpleJob(RuntimeMoniker.Net80)]
[HtmlExporter]
[MemoryDiagnoser]
public class AverageBenchmarks
{
#region Properties & Fields
private readonly List<ColorRGB[]> _colors3bpp;
private readonly List<ColorRGBA[]> _colors4bpp;
#endregion
#region Constructors
public AverageBenchmarks()
{
_colors3bpp = BenchmarkHelper.GetSampleData<ColorRGB>();
_colors4bpp = BenchmarkHelper.GetSampleData<ColorRGBA>();
}
#endregion
#region Methods
[Benchmark]
public ColorRGB[] PixelHelper_3BPP()
{
ColorRGB[] averages = new ColorRGB[_colors3bpp.Count];
for (int i = 0; i < _colors3bpp.Count; i++)
averages[i] = new ReadOnlySpan<ColorRGB>(_colors3bpp[i]).Average();
return averages;
}
[Benchmark]
public ColorRGBA[] PixelHelper_4BPP()
{
ColorRGBA[] averages = new ColorRGBA[_colors4bpp.Count];
for (int i = 0; i < _colors4bpp.Count; i++)
averages[i] = new ReadOnlySpan<ColorRGBA>(_colors4bpp[i]).Average();
return averages;
}
[Benchmark]
public ColorRGB[] Reference_3BPP()
{
ColorRGB[] averages = new ColorRGB[_colors3bpp.Count];
for (int i = 0; i < _colors3bpp.Count; i++)
averages[i] = ReferencePixelHelper.Average(new ReadOnlySpan<ColorRGB>(_colors3bpp[i]));
return averages;
}
[Benchmark]
public ColorRGBA[] Reference_4BPP()
{
ColorRGBA[] averages = new ColorRGBA[_colors4bpp.Count];
for (int i = 0; i < _colors4bpp.Count; i++)
averages[i] = ReferencePixelHelper.Average(new ReadOnlySpan<ColorRGBA>(_colors4bpp[i]));
return averages;
}
#endregion
}

View File

@ -0,0 +1,22 @@
using HPPH.System.Drawing;
namespace HPPH.Benchmark;
internal static class BenchmarkHelper
{
private const string SAMPLE_DATA_DIR = @"..\..\..\..\..\..\..\..\sample_data";
public static List<T[]> GetSampleData<T>()
where T : struct, IColor
{
if (!Directory.Exists(SAMPLE_DATA_DIR)) throw new Exception("sample data not found!");
List<T[]> colors = [];
IEnumerable<string> files = Directory.EnumerateFiles(SAMPLE_DATA_DIR, "*.png", SearchOption.AllDirectories);
foreach (string file in files)
colors.Add(ImageHelper.LoadImage(file).ConvertTo<T>().ToArray());
return colors;
}
}

View File

@ -1,6 +1,5 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using HPPH.System.Drawing;
namespace HPPH.Benchmark;
@ -11,7 +10,8 @@ public class ConvertBenchmarks
{
#region Properties & Fields
private readonly List<ColorRGB[]> _colors = [];
private readonly List<ColorRGB[]> _colors3bpp;
private readonly List<ColorRGBA[]> _colors4bpp;
#endregion
@ -19,13 +19,8 @@ public class ConvertBenchmarks
public ConvertBenchmarks()
{
if (!Directory.Exists(@"..\..\..\..\sample_data")) return;
_colors = [];
IEnumerable<string> files = Directory.EnumerateFiles(@"..\..\..\..\sample_data", "*.png", SearchOption.AllDirectories);
foreach (string file in files)
_colors.Add(ImageHelper.LoadImage(file).AsRefImage<ColorRGB>().ToArray());
_colors3bpp = BenchmarkHelper.GetSampleData<ColorRGB>();
_colors4bpp = BenchmarkHelper.GetSampleData<ColorRGBA>();
}
#endregion
@ -36,10 +31,8 @@ public class ConvertBenchmarks
public ColorBGR[] RGBToBGR()
{
ColorBGR[] result = [];
foreach (ColorRGB[] color in _colors)
{
foreach (ColorRGB[] color in _colors3bpp)
result = new ReadOnlySpan<ColorRGB>(color).Convert<ColorRGB, ColorBGR>();
}
return result;
}
@ -48,11 +41,45 @@ public class ConvertBenchmarks
public ColorBGRA[] RGBToBGRA()
{
ColorBGRA[] result = [];
foreach (ColorRGB[] color in _colors)
foreach (ColorRGB[] color in _colors3bpp)
result = new ReadOnlySpan<ColorRGB>(color).Convert<ColorRGB, ColorBGRA>();
return result;
}
[Benchmark]
public ColorABGR[] RGBAToABGR()
{
ColorABGR[] result = [];
foreach (ColorRGBA[] color in _colors4bpp)
result = new ReadOnlySpan<ColorRGBA>(color).Convert<ColorRGBA, ColorABGR>();
return result;
}
[Benchmark]
public ColorBGR[] ARGBToBGR()
{
ColorBGR[] result = [];
foreach (ColorRGBA[] color in _colors4bpp)
result = new ReadOnlySpan<ColorRGBA>(color).Convert<ColorRGBA, ColorBGR>();
return result;
}
[Benchmark]
public void RGBToBGR_InPlace()
{
foreach (ColorRGB[] color in _colors3bpp)
new Span<ColorRGB>(color).ConvertInPlace<ColorRGB, ColorBGR>();
}
[Benchmark]
public void RGBAToABGR_InPlace()
{
foreach (ColorRGBA[] color in _colors4bpp)
new Span<ColorRGBA>(color).ConvertInPlace<ColorRGBA, ColorABGR>();
}
#endregion
}

View File

@ -0,0 +1,72 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using HPPH.Reference;
namespace HPPH.Benchmark;
[SimpleJob(RuntimeMoniker.Net80)]
[HtmlExporter]
[MemoryDiagnoser]
public class MinMaxBenchmarks
{
#region Properties & Fields
private readonly List<ColorRGB[]> _colors3bpp;
private readonly List<ColorRGBA[]> _colors4bpp;
#endregion
#region Constructors
public MinMaxBenchmarks()
{
_colors3bpp = BenchmarkHelper.GetSampleData<ColorRGB>();
_colors4bpp = BenchmarkHelper.GetSampleData<ColorRGBA>();
}
#endregion
#region Methods
[Benchmark]
public IMinMax[] PixelHelper_3BPP()
{
IMinMax[] minMax = new IMinMax[_colors3bpp.Count];
for (int i = 0; i < _colors3bpp.Count; i++)
minMax[i] = new ReadOnlySpan<ColorRGB>(_colors3bpp[i]).MinMax();
return minMax;
}
[Benchmark]
public IMinMax[] PixelHelper_4BPP()
{
IMinMax[] minMax = new IMinMax[_colors4bpp.Count];
for (int i = 0; i < _colors4bpp.Count; i++)
minMax[i] = new ReadOnlySpan<ColorRGBA>(_colors4bpp[i]).MinMax();
return minMax;
}
[Benchmark]
public IMinMax[] Reference_3BPP()
{
IMinMax[] minMax = new IMinMax[_colors3bpp.Count];
for (int i = 0; i < _colors3bpp.Count; i++)
minMax[i] = ReferencePixelHelper.MinMax(new ReadOnlySpan<ColorRGB>(_colors3bpp[i]));
return minMax;
}
[Benchmark]
public IMinMax[] Reference_4BPP()
{
IMinMax[] minMax = new IMinMax[_colors4bpp.Count];
for (int i = 0; i < _colors4bpp.Count; i++)
minMax[i] = ReferencePixelHelper.MinMax(new ReadOnlySpan<ColorRGBA>(_colors4bpp[i]));
return minMax;
}
#endregion
}

View File

@ -1,4 +1,4 @@
using BenchmarkDotNet.Running;
using HPPH.Benchmark;
BenchmarkRunner.Run<ConvertBenchmarks>();
BenchmarkRunner.Run(typeof(Program).Assembly);

View File

@ -0,0 +1,60 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using HPPH.Reference;
namespace HPPH.Benchmark;
[SimpleJob(RuntimeMoniker.Net80)]
[HtmlExporter]
[MemoryDiagnoser]
public class SortBenchmarks
{
#region Properties & Fields
private readonly List<ColorRGB[]> _colors3bpp;
private readonly List<ColorRGBA[]> _colors4bpp;
#endregion
#region Constructors
public SortBenchmarks()
{
_colors3bpp = BenchmarkHelper.GetSampleData<ColorRGB>();
_colors4bpp = BenchmarkHelper.GetSampleData<ColorRGBA>();
}
#endregion
#region Methods
[Benchmark]
public void PixelHelper_3BPP()
{
foreach (ColorRGB[] colors in _colors3bpp)
new Span<ColorRGB>(colors).SortByRed();
}
[Benchmark]
public void PixelHelper_4BPP()
{
foreach (ColorRGBA[] colors in _colors4bpp)
new Span<ColorRGBA>(colors).SortByRed();
}
[Benchmark]
public void Reference_3BPP()
{
foreach (ColorRGB[] colors in _colors3bpp)
ReferencePixelHelper.SortByRed(new Span<ColorRGB>(colors));
}
[Benchmark]
public void Reference_4BPP()
{
foreach (ColorRGBA[] colors in _colors4bpp)
ReferencePixelHelper.SortByRed(new Span<ColorRGBA>(colors));
}
#endregion
}

View File

@ -0,0 +1,72 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using HPPH.Reference;
namespace HPPH.Benchmark;
[SimpleJob(RuntimeMoniker.Net80)]
[HtmlExporter]
[MemoryDiagnoser]
public class SumBenchmarks
{
#region Properties & Fields
private readonly List<ColorRGB[]> _colors3bpp;
private readonly List<ColorRGBA[]> _colors4bpp;
#endregion
#region Constructors
public SumBenchmarks()
{
_colors3bpp = BenchmarkHelper.GetSampleData<ColorRGB>();
_colors4bpp = BenchmarkHelper.GetSampleData<ColorRGBA>();
}
#endregion
#region Methods
[Benchmark]
public ISum[] PixelHelper_3BPP()
{
ISum[] sums = new ISum[_colors3bpp.Count];
for (int i = 0; i < _colors3bpp.Count; i++)
sums[i] = new ReadOnlySpan<ColorRGB>(_colors3bpp[i]).Sum();
return sums;
}
[Benchmark]
public ISum[] PixelHelper_4BPP()
{
ISum[] sums = new ISum[_colors4bpp.Count];
for (int i = 0; i < _colors4bpp.Count; i++)
sums[i] = new ReadOnlySpan<ColorRGBA>(_colors4bpp[i]).Sum();
return sums;
}
[Benchmark]
public ISum[] Reference_3BPP()
{
ISum[] sums = new ISum[_colors3bpp.Count];
for (int i = 0; i < _colors3bpp.Count; i++)
sums[i] = ReferencePixelHelper.Sum(new ReadOnlySpan<ColorRGB>(_colors3bpp[i]));
return sums;
}
[Benchmark]
public ISum[] Reference_4BPP()
{
ISum[] sums = new ISum[_colors4bpp.Count];
for (int i = 0; i < _colors4bpp.Count; i++)
sums[i] = ReferencePixelHelper.Sum(new ReadOnlySpan<ColorRGBA>(_colors4bpp[i]));
return sums;
}
#endregion
}