From cc8de89481645a5537e3aa4bc24b8383651d719e Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sun, 21 Jul 2024 21:36:26 +0200 Subject: [PATCH] Added a bunch of benchmarks --- HPPH.Benchmark/AverageBenchmarks.cs | 72 +++++++++++++++++++++++++++++ HPPH.Benchmark/BenchmarkHelper.cs | 22 +++++++++ HPPH.Benchmark/ConvertBenchmarks.cs | 53 +++++++++++++++------ HPPH.Benchmark/MinMaxBenchmarks.cs | 72 +++++++++++++++++++++++++++++ HPPH.Benchmark/Program.cs | 2 +- HPPH.Benchmark/SortBenchmarks.cs | 60 ++++++++++++++++++++++++ HPPH.Benchmark/SumBenchmarks.cs | 72 +++++++++++++++++++++++++++++ 7 files changed, 339 insertions(+), 14 deletions(-) create mode 100644 HPPH.Benchmark/AverageBenchmarks.cs create mode 100644 HPPH.Benchmark/BenchmarkHelper.cs create mode 100644 HPPH.Benchmark/MinMaxBenchmarks.cs create mode 100644 HPPH.Benchmark/SortBenchmarks.cs create mode 100644 HPPH.Benchmark/SumBenchmarks.cs diff --git a/HPPH.Benchmark/AverageBenchmarks.cs b/HPPH.Benchmark/AverageBenchmarks.cs new file mode 100644 index 0000000..9708606 --- /dev/null +++ b/HPPH.Benchmark/AverageBenchmarks.cs @@ -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 _colors3bpp; + private readonly List _colors4bpp; + + #endregion + + #region Constructors + + public AverageBenchmarks() + { + _colors3bpp = BenchmarkHelper.GetSampleData(); + _colors4bpp = BenchmarkHelper.GetSampleData(); + } + + #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(_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(_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(_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(_colors4bpp[i])); + + return averages; + } + + #endregion +} \ No newline at end of file diff --git a/HPPH.Benchmark/BenchmarkHelper.cs b/HPPH.Benchmark/BenchmarkHelper.cs new file mode 100644 index 0000000..13865aa --- /dev/null +++ b/HPPH.Benchmark/BenchmarkHelper.cs @@ -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 GetSampleData() + where T : struct, IColor + { + if (!Directory.Exists(SAMPLE_DATA_DIR)) throw new Exception("sample data not found!"); + + List colors = []; + + IEnumerable files = Directory.EnumerateFiles(SAMPLE_DATA_DIR, "*.png", SearchOption.AllDirectories); + foreach (string file in files) + colors.Add(ImageHelper.LoadImage(file).ConvertTo().ToArray()); + + return colors; + } +} \ No newline at end of file diff --git a/HPPH.Benchmark/ConvertBenchmarks.cs b/HPPH.Benchmark/ConvertBenchmarks.cs index 64dfdd1..ce171bb 100644 --- a/HPPH.Benchmark/ConvertBenchmarks.cs +++ b/HPPH.Benchmark/ConvertBenchmarks.cs @@ -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 _colors = []; + private readonly List _colors3bpp; + private readonly List _colors4bpp; #endregion @@ -19,13 +19,8 @@ public class ConvertBenchmarks public ConvertBenchmarks() { - if (!Directory.Exists(@"..\..\..\..\sample_data")) return; - - _colors = []; - - IEnumerable files = Directory.EnumerateFiles(@"..\..\..\..\sample_data", "*.png", SearchOption.AllDirectories); - foreach (string file in files) - _colors.Add(ImageHelper.LoadImage(file).AsRefImage().ToArray()); + _colors3bpp = BenchmarkHelper.GetSampleData(); + _colors4bpp = BenchmarkHelper.GetSampleData(); } #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(color).Convert(); - } 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(color).Convert(); return result; } + [Benchmark] + public ColorABGR[] RGBAToABGR() + { + ColorABGR[] result = []; + foreach (ColorRGBA[] color in _colors4bpp) + result = new ReadOnlySpan(color).Convert(); + + return result; + } + + [Benchmark] + public ColorBGR[] ARGBToBGR() + { + ColorBGR[] result = []; + foreach (ColorRGBA[] color in _colors4bpp) + result = new ReadOnlySpan(color).Convert(); + + return result; + } + + [Benchmark] + public void RGBToBGR_InPlace() + { + foreach (ColorRGB[] color in _colors3bpp) + new Span(color).ConvertInPlace(); + } + + [Benchmark] + public void RGBAToABGR_InPlace() + { + foreach (ColorRGBA[] color in _colors4bpp) + new Span(color).ConvertInPlace(); + } + #endregion } \ No newline at end of file diff --git a/HPPH.Benchmark/MinMaxBenchmarks.cs b/HPPH.Benchmark/MinMaxBenchmarks.cs new file mode 100644 index 0000000..1774677 --- /dev/null +++ b/HPPH.Benchmark/MinMaxBenchmarks.cs @@ -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 _colors3bpp; + private readonly List _colors4bpp; + + #endregion + + #region Constructors + + public MinMaxBenchmarks() + { + _colors3bpp = BenchmarkHelper.GetSampleData(); + _colors4bpp = BenchmarkHelper.GetSampleData(); + } + + #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(_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(_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(_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(_colors4bpp[i])); + + return minMax; + } + + #endregion +} \ No newline at end of file diff --git a/HPPH.Benchmark/Program.cs b/HPPH.Benchmark/Program.cs index 6c7bb39..87e23c3 100644 --- a/HPPH.Benchmark/Program.cs +++ b/HPPH.Benchmark/Program.cs @@ -1,4 +1,4 @@ using BenchmarkDotNet.Running; using HPPH.Benchmark; -BenchmarkRunner.Run(); +BenchmarkRunner.Run(typeof(Program).Assembly); \ No newline at end of file diff --git a/HPPH.Benchmark/SortBenchmarks.cs b/HPPH.Benchmark/SortBenchmarks.cs new file mode 100644 index 0000000..934060c --- /dev/null +++ b/HPPH.Benchmark/SortBenchmarks.cs @@ -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 _colors3bpp; + private readonly List _colors4bpp; + + #endregion + + #region Constructors + + public SortBenchmarks() + { + _colors3bpp = BenchmarkHelper.GetSampleData(); + _colors4bpp = BenchmarkHelper.GetSampleData(); + } + + #endregion + + #region Methods + + [Benchmark] + public void PixelHelper_3BPP() + { + foreach (ColorRGB[] colors in _colors3bpp) + new Span(colors).SortByRed(); + } + + [Benchmark] + public void PixelHelper_4BPP() + { + foreach (ColorRGBA[] colors in _colors4bpp) + new Span(colors).SortByRed(); + } + + [Benchmark] + public void Reference_3BPP() + { + foreach (ColorRGB[] colors in _colors3bpp) + ReferencePixelHelper.SortByRed(new Span(colors)); + } + + [Benchmark] + public void Reference_4BPP() + { + foreach (ColorRGBA[] colors in _colors4bpp) + ReferencePixelHelper.SortByRed(new Span(colors)); + } + + #endregion +} \ No newline at end of file diff --git a/HPPH.Benchmark/SumBenchmarks.cs b/HPPH.Benchmark/SumBenchmarks.cs new file mode 100644 index 0000000..bc7d105 --- /dev/null +++ b/HPPH.Benchmark/SumBenchmarks.cs @@ -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 _colors3bpp; + private readonly List _colors4bpp; + + #endregion + + #region Constructors + + public SumBenchmarks() + { + _colors3bpp = BenchmarkHelper.GetSampleData(); + _colors4bpp = BenchmarkHelper.GetSampleData(); + } + + #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(_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(_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(_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(_colors4bpp[i])); + + return sums; + } + + #endregion +} \ No newline at end of file