diff --git a/tests/ImageSharp.Benchmarks/Codecs/Exr/DecodeExr.cs b/tests/ImageSharp.Benchmarks/Codecs/Exr/DecodeExr.cs index f4bf7c83e9..47211d72e0 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Exr/DecodeExr.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Exr/DecodeExr.cs @@ -19,6 +19,9 @@ public class DecodeExr private byte[] imageBytes; + // Both libraries read from this stream. Each call rewinds it, so the results measure the decoders only. + private MemoryStream inputStream; + private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); [Params(TestImages.Exr.Benchmark)] @@ -31,27 +34,38 @@ public void ReadImages() new ExrConfigurationModule().Configure(this.configuration); this.imageBytes ??= File.ReadAllBytes(this.TestImageFullPath); + this.inputStream ??= new MemoryStream(this.imageBytes, false); + } + + /// + /// Releases the shared input stream. + /// + [GlobalCleanup] + public void Cleanup() + { + this.inputStream.Dispose(); + this.inputStream = null; } [Benchmark(Description = "Magick Exr")] public uint ExrImageMagick() { MagickReadSettings settings = new() { Format = MagickFormat.Exr }; - using MemoryStream memoryStream = new(this.imageBytes); - using MagickImage image = new(memoryStream, settings); + this.inputStream.Position = 0; + using MagickImage image = new(this.inputStream, settings); return image.Width; } [Benchmark(Description = "ImageSharp Exr")] public int ExrImageSharp() { - using MemoryStream memoryStream = new(this.imageBytes); - using Image image = Image.Load(memoryStream); + this.inputStream.Position = 0; + using Image image = Image.Load(this.inputStream); return image.Height; } - /* Results 28.10.2026 - BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9711) + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 11.0.100-preview.7.26381.103 [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 @@ -60,10 +74,10 @@ .NET SDK 11.0.100-preview.7.26381.103 Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 WarmupCount=3 - | Method | TestImage | Mean | Error | StdDev | Gen0 | Gen1 | Gen2 | Allocated | - | ----------------- | ----------------------------- | ---------: | ---------: | ---------: | --------: | --------: | --------: | ----------: | - | 'Magick Exr' | Exr/Calliphora_benchmark.exr | 19.17 ms | 1.278 ms | 0.070 ms | - | - | - | 5.07 KB | - | 'ImageSharp Exr' | Exr/Calliphora_benchmark.exr | 25.71 ms | 1.120 ms | 0.061 ms | 31.2500 | 31.2500 | 31.2500 | 40.01 KB | + | Method | TestImage | Mean | Error | StdDev | Gen0 | Gen1 | Gen2 | Allocated | + |----------------- |----------------------------- |---------:|---------:|---------:|--------:|--------:|--------:|----------:| + | 'Magick Exr' | Exr/Calliphora_benchmark.exr | 19.19 ms | 3.300 ms | 0.181 ms | - | - | - | 5.02 KB | + | 'ImageSharp Exr' | Exr/Calliphora_benchmark.exr | 35.02 ms | 0.561 ms | 0.031 ms | 71.4286 | 71.4286 | 71.4286 | 36.58 KB | Allocated reports managed memory only. ImageSharp writes Rgba32 output; Magick uses Q16 samples. This comparison does not measure HDR output. diff --git a/tests/ImageSharp.Benchmarks/Codecs/Gif/DecodeGif.cs b/tests/ImageSharp.Benchmarks/Codecs/Gif/DecodeGif.cs index 3430f1d91c..35c39781e7 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Gif/DecodeGif.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Gif/DecodeGif.cs @@ -18,11 +18,28 @@ public class DecodeGif { private byte[] gifBytes; + // All three libraries read from this stream. Each call rewinds it, so the results measure the decoders only. + private MemoryStream inputStream; + private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); [GlobalSetup] - public void ReadImages() => this.gifBytes ??= File.ReadAllBytes(this.TestImageFullPath); + public void ReadImages() + { + this.gifBytes ??= File.ReadAllBytes(this.TestImageFullPath); + this.inputStream ??= new MemoryStream(this.gifBytes, false); + } + + /// + /// Releases the shared input stream. + /// + [GlobalCleanup] + public void Cleanup() + { + this.inputStream.Dispose(); + this.inputStream = null; + } [Params(TestImages.Gif.Cheers)] public string TestImage { get; set; } @@ -34,8 +51,8 @@ private string TestImageFullPath [Benchmark(Baseline = true, Description = "System.Drawing Gif")] public SDSize GifSystemDrawing() { - using MemoryStream memoryStream = new(this.gifBytes); - using SDImage image = SDImage.FromStream(memoryStream); + this.inputStream.Position = 0; + using SDImage image = SDImage.FromStream(this.inputStream); // Select and render every animation frame. Reading Size alone does not request the later frames. // Reuse one destination bitmap so rendering forces pixel decoding without allocating one bitmap per frame. @@ -60,9 +77,9 @@ public SDSize GifSystemDrawing() [Benchmark(Description = "Magick Gif")] public int GifMagick() { - using MemoryStream memoryStream = new(this.gifBytes); + this.inputStream.Position = 0; MagickReadSettings settings = new() { Format = MagickFormat.Gif }; - using MagickImageCollection images = new(memoryStream, settings); + using MagickImageCollection images = new(this.inputStream, settings); // GIF frames can store only changed rectangles. Compose them into full frames, as ImageSharp does. images.Coalesce(); @@ -72,13 +89,13 @@ public int GifMagick() [Benchmark(Description = "ImageSharp Gif")] public Size GifImageSharp() { - using MemoryStream memoryStream = new(this.gifBytes); - using Image image = Image.Load(memoryStream); + this.inputStream.Position = 0; + using Image image = Image.Load(this.inputStream); return new Size(image.Width, image.Height); } - /* Results 28.10.2026 - BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9711) + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 11.0.100-preview.7.26381.103 [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 @@ -87,11 +104,11 @@ .NET SDK 11.0.100-preview.7.26381.103 Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 WarmupCount=3 - | Method | TestImage | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | - | --------------------- | --------------- | ---------: | ---------: | --------: | ------: | --------: | ----------: | ----------: | ----------: | -----------: | ------------: | - | 'System.Drawing Gif' | Gif/cheers.gif | 127.9 ms | 22.28 ms | 1.22 ms | 1.00 | 0.01 | - | - | - | 3011.91 KB | 1.00 | - | 'Magick Gif' | Gif/cheers.gif | 507.8 ms | 37.76 ms | 2.07 ms | 3.97 | 0.04 | - | - | - | 353.77 KB | 0.12 | - | 'ImageSharp Gif' | Gif/cheers.gif | 101.9 ms | 10.78 ms | 0.59 ms | 0.80 | 0.01 | 2400.0000 | 2400.0000 | 2400.0000 | 339.57 KB | 0.11 | + | Method | TestImage | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | + |--------------------- |--------------- |---------:|---------:|--------:|------:|--------:|----------:|----------:|----------:|-----------:|------------:| + | 'System.Drawing Gif' | Gif/cheers.gif | 126.6 ms | 12.87 ms | 0.71 ms | 1.00 | 0.01 | - | - | - | 3011.85 KB | 1.00 | + | 'Magick Gif' | Gif/cheers.gif | 517.2 ms | 84.38 ms | 4.63 ms | 4.08 | 0.04 | - | - | - | 353.95 KB | 0.12 | + | 'ImageSharp Gif' | Gif/cheers.gif | 101.3 ms | 15.18 ms | 0.83 ms | 0.80 | 0.01 | 2400.0000 | 2400.0000 | 2400.0000 | 339.54 KB | 0.11 | Allocated reports managed memory only. All methods decode the 93 frames of cheers.gif. System.Drawing renders into one reusable bitmap; Magick and ImageSharp retain full frames. diff --git a/tests/ImageSharp.Benchmarks/Codecs/Gif/EncodeGif.cs b/tests/ImageSharp.Benchmarks/Codecs/Gif/EncodeGif.cs index 70ce37ab35..17f8834958 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Gif/EncodeGif.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Gif/EncodeGif.cs @@ -17,6 +17,10 @@ public abstract class EncodeGif private Image bmpCore; private MagickImageCollection magickImage; + // Both libraries write into this stream. Each call rewinds it, so after the first call it has the full capacity, + // and the results measure the encoders instead of the growth of a new stream. + private MemoryStream outputStream; + protected abstract GifEncoder Encoder { get; } [Params(TestImages.Gif.Leo, TestImages.Gif.Cheers)] @@ -44,6 +48,8 @@ public void ReadImages() { frame.ColorType = ColorType.TrueColorAlpha; } + + this.outputStream = new MemoryStream(); } } @@ -54,20 +60,21 @@ public void Cleanup() this.bmpStream = null; this.bmpCore.Dispose(); this.magickImage.Dispose(); + this.outputStream.Dispose(); } [Benchmark(Description = "ImageSharp Gif")] public void GifImageSharp() { - using MemoryStream memoryStream = new(); - this.bmpCore.SaveAsGif(memoryStream, this.Encoder); + this.outputStream.Position = 0; + this.bmpCore.SaveAsGif(this.outputStream, this.Encoder); } [Benchmark(Baseline = true, Description = "Magick.NET Gif")] public void GifMagickNet() { - using MemoryStream ms = new(); - this.magickImage.Write(ms, MagickFormat.Gif); + this.outputStream.Position = 0; + this.magickImage.Write(this.outputStream, MagickFormat.Gif); } } @@ -75,20 +82,21 @@ public class EncodeGif_DefaultEncoder : EncodeGif { protected override GifEncoder Encoder => new(); - /* Results 28.10.2026 - BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9711) + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 11.0.100-preview.7.26381.103 [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 DefaultJob : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 - | Method | TestImage | Mean | Error | StdDev | Ratio | - | ----------------- | --------------- | ----------: | ---------: | ---------: | ------: | - | 'ImageSharp Gif' | Gif/cheers.gif | 156.68 ms | 1.129 ms | 1.056 ms | 1.03 | - | 'Magick.NET Gif' | Gif/cheers.gif | 151.72 ms | 0.407 ms | 0.381 ms | 1.00 | - | | | | | | | - | 'ImageSharp Gif' | Gif/leo.gif | 16.16 ms | 0.067 ms | 0.063 ms | 1.35 | - | 'Magick.NET Gif' | Gif/leo.gif | 12.00 ms | 0.054 ms | 0.048 ms | 1.00 | + + | Method | TestImage | Mean | Error | StdDev | Ratio | RatioSD | + |----------------- |--------------- |----------:|---------:|---------:|------:|--------:| + | 'ImageSharp Gif' | Gif/cheers.gif | 155.87 ms | 1.424 ms | 1.332 ms | 1.00 | 0.01 | + | 'Magick.NET Gif' | Gif/cheers.gif | 156.23 ms | 0.911 ms | 0.761 ms | 1.00 | 0.01 | + | | | | | | | | + | 'ImageSharp Gif' | Gif/leo.gif | 17.35 ms | 0.318 ms | 0.402 ms | 1.43 | 0.04 | + | 'Magick.NET Gif' | Gif/leo.gif | 12.15 ms | 0.191 ms | 0.149 ms | 1.00 | 0.02 | Both encoders receive full animation frames. Magick source palettes are removed before measurement so its writer must quantize the pixels. The existing default job has no memory diagnoser, so this table does not report allocations. @@ -101,4 +109,24 @@ public class EncodeGif_CoarsePaletteEncoder : EncodeGif { Quantizer = new WebSafePaletteQuantizer(new QuantizerOptions { Dither = KnownDitherings.Bayer4x4, ColorMatchingMode = ColorMatchingMode.Coarse }) }; + + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) + AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores + .NET SDK 11.0.100-preview.7.26381.103 + [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 + DefaultJob : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 + + + | Method | TestImage | Mean | Error | StdDev | Ratio | + |----------------- |--------------- |----------:|---------:|---------:|------:| + | 'ImageSharp Gif' | Gif/cheers.gif | 264.28 ms | 1.677 ms | 1.487 ms | 1.74 | + | 'Magick.NET Gif' | Gif/cheers.gif | 151.64 ms | 0.672 ms | 0.629 ms | 1.00 | + | | | | | | | + | 'ImageSharp Gif' | Gif/leo.gif | 25.34 ms | 0.077 ms | 0.072 ms | 2.15 | + | 'Magick.NET Gif' | Gif/leo.gif | 11.77 ms | 0.019 ms | 0.018 ms | 1.00 | + + Both encoders receive full animation frames. Magick source palettes are removed before measurement so its writer must quantize the pixels. + The existing default job has no memory diagnoser, so this table does not report allocations. + */ } diff --git a/tests/ImageSharp.Benchmarks/Codecs/Heif/DecodeHeif.cs b/tests/ImageSharp.Benchmarks/Codecs/Heif/DecodeHeif.cs index 85331ba688..93b24695b3 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Heif/DecodeHeif.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Heif/DecodeHeif.cs @@ -19,6 +19,9 @@ public class DecodeHeif { private byte[] avifBytes; + // Both libraries read from this stream. Each call rewinds it, so the results measure the decoders only. + private MemoryStream inputStream; + /// /// Gets or sets the AVIF test image. /// @@ -31,7 +34,11 @@ public class DecodeHeif /// Reads the encoded test image. /// [GlobalSetup] - public void ReadImages() => this.avifBytes ??= File.ReadAllBytes(this.TestImageFullPath); + public void ReadImages() + { + this.avifBytes ??= File.ReadAllBytes(this.TestImageFullPath); + this.inputStream ??= new MemoryStream(this.avifBytes, false); + } /// /// Pins the benchmark process to the first core and reads the encoded test image. @@ -44,6 +51,16 @@ public void ReadImagesOnSingleCore() this.ReadImages(); } + /// + /// Releases the shared input stream. + /// + [GlobalCleanup] + public void Cleanup() + { + this.inputStream.Dispose(); + this.inputStream = null; + } + /// /// Decodes the image with Magick.NET. /// @@ -52,8 +69,8 @@ public void ReadImagesOnSingleCore() public uint AvifMagick() { MagickReadSettings settings = new() { Format = MagickFormat.Avif }; - using MemoryStream memoryStream = new(this.avifBytes); - using MagickImage image = new(memoryStream, settings); + this.inputStream.Position = 0; + using MagickImage image = new(this.inputStream, settings); return image.Width; } @@ -71,13 +88,13 @@ public uint AvifMagick() [Benchmark(Description = "ImageSharp Avif")] public int Avif() { - using MemoryStream memoryStream = new(this.avifBytes); - using Image image = Image.Load(memoryStream); + this.inputStream.Position = 0; + using Image image = Image.Load(this.inputStream); return image.Height; } - /* Results 28.10.2026 - BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9711) + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 11.0.100-preview.7.26381.103 [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 @@ -86,15 +103,15 @@ .NET SDK 11.0.100-preview.7.26381.103 Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 WarmupCount=3 - | Method | TestImage | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | - | ------------------------- | ----------------------------- | ---------: | ----------: | ---------: | ------: | --------: | --------: | --------: | --------: | ----------: | ------------: | - | 'Magick Avif' | Heif/Irvine_CA.avif | 20.11 ms | 6.352 ms | 0.348 ms | 1.00 | 0.02 | - | - | - | 5.07 KB | 1.00 | - | 'Magick Avif SingleCore' | Heif/Irvine_CA.avif | 19.34 ms | 4.067 ms | 0.223 ms | 0.96 | 0.02 | - | - | - | 5.08 KB | 1.00 | - | 'ImageSharp Avif' | Heif/Irvine_CA.avif | 11.63 ms | 0.404 ms | 0.022 ms | 0.58 | 0.01 | 78.1250 | 62.5000 | 46.8750 | 560.91 KB | 110.54 | - | | | | | | | | | | | | | - | 'Magick Avif' | Heif/libavif-kodim23-8b.avif | 21.41 ms | 3.719 ms | 0.204 ms | 1.00 | 0.01 | - | - | - | 5.07 KB | 1.00 | - | 'Magick Avif SingleCore' | Heif/libavif-kodim23-8b.avif | 21.76 ms | 11.596 ms | 0.636 ms | 1.02 | 0.03 | - | - | - | 5.08 KB | 1.00 | - | 'ImageSharp Avif' | Heif/libavif-kodim23-8b.avif | 10.53 ms | 0.505 ms | 0.028 ms | 0.49 | 0.00 | 78.1250 | 62.5000 | 46.8750 | 541.21 KB | 106.66 | + | Method | TestImage | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | + |------------------------- |----------------------------- |---------:|----------:|---------:|------:|--------:|--------:|--------:|--------:|----------:|------------:| + | 'Magick Avif' | Heif/Irvine_CA.avif | 21.79 ms | 7.909 ms | 0.434 ms | 1.00 | 0.02 | - | - | - | 5.02 KB | 1.00 | + | 'Magick Avif SingleCore' | Heif/Irvine_CA.avif | 24.12 ms | 57.377 ms | 3.145 ms | 1.11 | 0.13 | - | - | - | 5.03 KB | 1.00 | + | 'ImageSharp Avif' | Heif/Irvine_CA.avif | 13.25 ms | 2.303 ms | 0.126 ms | 0.61 | 0.01 | 78.1250 | 62.5000 | 46.8750 | 560.9 KB | 111.74 | + | | | | | | | | | | | | | + | 'Magick Avif' | Heif/libavif-kodim23-8b.avif | 23.65 ms | 4.813 ms | 0.264 ms | 1.00 | 0.01 | - | - | - | 5.02 KB | 1.00 | + | 'Magick Avif SingleCore' | Heif/libavif-kodim23-8b.avif | 23.43 ms | 2.761 ms | 0.151 ms | 0.99 | 0.01 | - | - | - | 5.02 KB | 1.00 | + | 'ImageSharp Avif' | Heif/libavif-kodim23-8b.avif | 11.59 ms | 10.820 ms | 0.593 ms | 0.49 | 0.02 | 78.1250 | 62.5000 | 46.8750 | 541.18 KB | 107.81 | Allocated reports managed memory only. The existing SingleCore method pins only its own Magick process. Other methods retain their existing threading behavior. diff --git a/tests/ImageSharp.Benchmarks/Codecs/Heif/EncodeHeif.cs b/tests/ImageSharp.Benchmarks/Codecs/Heif/EncodeHeif.cs index 4a10393353..5092ef5ab1 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Heif/EncodeHeif.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Heif/EncodeHeif.cs @@ -21,6 +21,10 @@ public class EncodeHeif private MagickImage avifMagick; private Image avif; + // Both libraries write into this stream. Each call rewinds it, so after the first call it has the full capacity, + // and the results measure the encoders instead of the growth of a new stream. + private MemoryStream outputStream; + /// /// Gets or sets the source test image. /// @@ -44,6 +48,7 @@ public void ReadImages() // is also the default of the ImageSharp encoder, so neither encoder sets a tune. this.avifMagick.Settings.SetDefine(MagickFormat.Heic, "speed", "6"); this.avifMagick.Settings.SetDefine(MagickFormat.Heic, "chroma", "420"); + this.outputStream = new MemoryStream(); } } @@ -59,13 +64,14 @@ public void ReadImagesOnSingleCore() } /// - /// Releases the source images. + /// Releases the source images and the shared output stream. /// [GlobalCleanup] public void Cleanup() { this.avif.Dispose(); this.avifMagick.Dispose(); + this.outputStream.Dispose(); } /// @@ -74,9 +80,9 @@ public void Cleanup() [Benchmark(Baseline = true, Description = "Magick Avif")] public void MagickAvif() { - using MemoryStream memoryStream = new(); + this.outputStream.Position = 0; this.avifMagick.Quality = 75; - this.avifMagick.Write(memoryStream, MagickFormat.Avif); + this.avifMagick.Write(this.outputStream, MagickFormat.Avif); } /// @@ -91,8 +97,8 @@ public void MagickAvif() [Benchmark(Description = "ImageSharp Avif")] public void ImageSharpAvif() { - using MemoryStream memoryStream = new(); - this.avif.Save(memoryStream, new HeifEncoder + this.outputStream.Position = 0; + this.avif.Save(this.outputStream, new HeifEncoder { Quality = 75, Speed = HeifEncodingSpeed.Level6, @@ -100,8 +106,8 @@ public void ImageSharpAvif() }); } - /* Results 28.10.2026 - BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9711) + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 11.0.100-preview.7.26381.103 [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 @@ -110,15 +116,15 @@ .NET SDK 11.0.100-preview.7.26381.103 Arguments=/p:DebugType=portable IterationCount=15 LaunchCount=1 WarmupCount=5 - | Method | TestImage | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | - | ------------------------- | --------------- | ---------: | ---------: | ---------: | ------: | --------: | ----------: | ----------: | ----------: | -----------: | ------------: | - | 'Magick Avif' | Png/Bike.png | 228.6 ms | 2.37 ms | 2.10 ms | 1.00 | 0.01 | - | - | - | 65.55 KB | 1.00 | - | 'Magick Avif SingleCore' | Png/Bike.png | 426.1 ms | 19.79 ms | 17.54 ms | 1.86 | 0.08 | - | - | - | 71.01 KB | 1.08 | - | 'ImageSharp Avif' | Png/Bike.png | 233.6 ms | 14.79 ms | 13.84 ms | 1.02 | 0.06 | 1000.0000 | 1000.0000 | 1000.0000 | 2118.65 KB | 32.32 | - | | | | | | | | | | | | | - | 'Magick Avif' | Png/splash.png | 188.9 ms | 4.13 ms | 3.45 ms | 1.00 | 0.02 | - | - | - | 60.55 KB | 1.00 | - | 'Magick Avif SingleCore' | Png/splash.png | 233.8 ms | 3.52 ms | 2.94 ms | 1.24 | 0.03 | - | - | - | 61.51 KB | 1.02 | - | 'ImageSharp Avif' | Png/splash.png | 176.3 ms | 19.17 ms | 17.93 ms | 0.93 | 0.09 | 3000.0000 | 3000.0000 | 3000.0000 | 2703.02 KB | 44.64 | + | Method | TestImage | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | + |------------------------- |--------------- |---------:|---------:|---------:|------:|--------:|----------:|----------:|----------:|----------:|------------:| + | 'Magick Avif' | Png/Bike.png | 223.8 ms | 18.78 ms | 17.57 ms | 1.01 | 0.12 | - | - | - | 9.47 KB | 1.00 | + | 'Magick Avif SingleCore' | Png/Bike.png | 408.3 ms | 3.93 ms | 3.28 ms | 1.84 | 0.16 | - | - | - | 15.12 KB | 1.60 | + | 'ImageSharp Avif' | Png/Bike.png | 222.9 ms | 1.89 ms | 1.68 ms | 1.00 | 0.09 | 3000.0000 | 3000.0000 | 3000.0000 | 2062.4 KB | 217.74 | + | | | | | | | | | | | | | + | 'Magick Avif' | Png/splash.png | 159.5 ms | 1.71 ms | 1.51 ms | 1.00 | 0.01 | - | - | - | 7.41 KB | 1.00 | + | 'Magick Avif SingleCore' | Png/splash.png | 238.5 ms | 4.45 ms | 4.16 ms | 1.50 | 0.03 | - | - | - | 5.86 KB | 0.79 | + | 'ImageSharp Avif' | Png/splash.png | 146.0 ms | 2.07 ms | 1.73 ms | 0.92 | 0.01 | 3000.0000 | 3000.0000 | 3000.0000 | 2647.5 KB | 357.19 | Allocated reports managed memory only. Run with --warmupCount 5 --iterationCount 15. Three measurements gave unstable ImageSharp means. diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs index 74971ec1dd..1c08236542 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpegParseStreamOnly.cs @@ -22,23 +22,39 @@ public class DecodeJpegParseStreamOnly private byte[] jpegBytes; + // Both methods read from this stream. Each call rewinds it, so the results measure the decoders only. + private MemoryStream inputStream; + [GlobalSetup] public void Setup() - => this.jpegBytes = File.ReadAllBytes(this.TestImageFullPath); + { + this.jpegBytes = File.ReadAllBytes(this.TestImageFullPath); + this.inputStream = new MemoryStream(this.jpegBytes, false); + } + + /// + /// Releases the shared input stream. + /// + [GlobalCleanup] + public void Cleanup() + { + this.inputStream.Dispose(); + this.inputStream = null; + } [Benchmark(Baseline = true, Description = "System.Drawing FULL")] public SDSize JpegSystemDrawing() { - using MemoryStream memoryStream = new(this.jpegBytes); - using System.Drawing.Image image = System.Drawing.Image.FromStream(memoryStream); + this.inputStream.Position = 0; + using System.Drawing.Image image = System.Drawing.Image.FromStream(this.inputStream); return image.Size; } [Benchmark(Description = "JpegDecoderCore.ParseStream")] public void ParseStream() { - using MemoryStream memoryStream = new(this.jpegBytes); - using BufferedReadStream bufferedStream = new(Configuration.Default, memoryStream); + this.inputStream.Position = 0; + using BufferedReadStream bufferedStream = new(Configuration.Default, this.inputStream); JpegDecoderOptions options = new(new DecoderOptions(Configuration.Default) { SkipMetadata = true }); using JpegDecoderCore decoder = new(options); @@ -66,26 +82,22 @@ public override void PrepareForDecoding() { } } -} -/* -BenchmarkDotNet=v0.13.0, OS=Windows 10.0.19042.1083 (20H2/October2020Update) -Intel Core i7-6700K CPU 4.00GHz (Skylake), 1 CPU, 8 logical and 4 physical cores -.NET SDK=6.0.100-preview.3.21202.5 -[Host] : .NET Core 3.1.13 (CoreCLR 4.700.21.11102, CoreFX 4.700.21.11602), X64 RyuJIT -Job-VAJCIU : .NET Core 2.1.26 (CoreCLR 4.6.29812.02, CoreFX 4.6.29812.01), X64 RyuJIT -Job-INPXCR : .NET Core 3.1.13 (CoreCLR 4.700.21.11102, CoreFX 4.700.21.11602), X64 RyuJIT -Job-JRCLOJ : .NET Framework 4.8 (4.8.4390.0), X64 RyuJIT - -IterationCount=3 LaunchCount=1 WarmupCount=3 -| Method | Job | Runtime | TestImage | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated | -|---------------------------- |----------- |--------------------- |---------------------- |---------:|----------:|----------:|------:|--------:|------:|------:|----------:| -| 'System.Drawing FULL' | Job-VAJCIU | .NET Core 2.1 | Jpg/baseline/Lake.jpg | 5.196 ms | 0.7520 ms | 0.0412 ms | 1.00 | 46.8750 | - | - | 210,768 B | -| JpegDecoderCore.ParseStream | Job-VAJCIU | .NET Core 2.1 | Jpg/baseline/Lake.jpg | 3.467 ms | 0.0784 ms | 0.0043 ms | 0.67 | - | - | - | 12,416 B | -| | | | | | | | | | | | | -| 'System.Drawing FULL' | Job-INPXCR | .NET Core 3.1 | Jpg/baseline/Lake.jpg | 5.201 ms | 0.4105 ms | 0.0225 ms | 1.00 | - | - | - | 183 B | -| JpegDecoderCore.ParseStream | Job-INPXCR | .NET Core 3.1 | Jpg/baseline/Lake.jpg | 3.349 ms | 0.0468 ms | 0.0026 ms | 0.64 | - | - | - | 12,408 B | -| | | | | | | | | | | | | -| 'System.Drawing FULL' | Job-JRCLOJ | .NET Framework 4.7.2 | Jpg/baseline/Lake.jpg | 5.164 ms | 0.6524 ms | 0.0358 ms | 1.00 | 46.8750 | - | - | 211,571 B | -| JpegDecoderCore.ParseStream | Job-JRCLOJ | .NET Framework 4.7.2 | Jpg/baseline/Lake.jpg | 4.548 ms | 0.3357 ms | 0.0184 ms | 0.88 | - | - | - | 12,480 B | -*/ + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) + AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores + .NET SDK 11.0.100-preview.7.26381.103 + [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 + Job-RCQXAA : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 + + Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 + WarmupCount=3 + + | Method | TestImage | Mean | Error | StdDev | Ratio | RatioSD | Allocated | Alloc Ratio | + |---------------------------- |---------------------- |---------:|----------:|----------:|------:|--------:|----------:|------------:| + | 'System.Drawing FULL' | Jpg/baseline/Lake.jpg | 2.761 ms | 0.6452 ms | 0.0354 ms | 1.00 | 0.02 | 330 B | 1.00 | + | JpegDecoderCore.ParseStream | Jpg/baseline/Lake.jpg | 1.146 ms | 0.0844 ms | 0.0046 ms | 0.42 | 0.00 | 17085 B | 51.77 | + + Allocated reports managed memory only. + */ +} diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpeg_ImageSpecific.cs b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpeg_ImageSpecific.cs index 1f1e397ea3..ccc6d0fd23 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpeg_ImageSpecific.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/DecodeJpeg_ImageSpecific.cs @@ -19,6 +19,9 @@ public class DecodeJpeg_ImageSpecific { private byte[] jpegBytes; + // All three libraries read from this stream. Each call rewinds it, so the results measure the decoders only. + private MemoryStream inputStream; + private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); #pragma warning disable SA1115 @@ -44,6 +47,17 @@ public class DecodeJpeg_ImageSpecific public void ReadImages() { this.jpegBytes ??= File.ReadAllBytes(this.TestImageFullPath); + this.inputStream ??= new MemoryStream(this.jpegBytes, false); + } + + /// + /// Releases the shared input stream. + /// + [GlobalCleanup] + public void Cleanup() + { + this.inputStream.Dispose(); + this.inputStream = null; } /// @@ -57,18 +71,18 @@ public void ReadImages() [Benchmark] public SDSize SystemDrawing() { - using MemoryStream memoryStream = new(this.jpegBytes); + this.inputStream.Position = 0; // GDI+ can retain native CMYK despite this request. The issue-129.jpg result is annotated below. - using SDImage image = SDImage.FromStream(memoryStream, useEmbeddedColorManagement: true, validateImageData: true); + using SDImage image = SDImage.FromStream(this.inputStream, useEmbeddedColorManagement: true, validateImageData: true); return image.Size; } [Benchmark] public Size ImageSharp() { - using MemoryStream memoryStream = new(this.jpegBytes); - using Image image = Image.Load(new DecoderOptions(Configuration.Default) { SkipMetadata = true }, memoryStream); + this.inputStream.Position = 0; + using Image image = Image.Load(new DecoderOptions(Configuration.Default) { SkipMetadata = true }, this.inputStream); return new Size(image.Width, image.Height); } @@ -79,8 +93,10 @@ public Size ImageSharp() [Benchmark(Baseline = true)] public Size SkiaSharp() { - using MemoryStream memoryStream = new(this.jpegBytes); - using SKCodec codec = SKCodec.Create(memoryStream); + this.inputStream.Position = 0; + + // SKCodec.Create(Stream) disposes the stream with the codec. This wrapper keeps the shared stream open. + using SKCodec codec = SKCodec.Create(new SKManagedStream(this.inputStream, false)); using SKColorSpace colorSpace = SKColorSpace.CreateSrgb(); // An explicit sRGB destination requests color conversion instead of retaining the source profile's samples. @@ -89,8 +105,8 @@ public Size SkiaSharp() return new Size(image.Width, image.Height); } - /* Results 28.10.2026 - BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9711) + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 11.0.100-preview.7.26381.103 [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 @@ -99,31 +115,31 @@ .NET SDK 11.0.100-preview.7.26381.103 Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 WarmupCount=3 - | Method | TestImage | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | - | -------------- | -------------------------------------------- | -----------: | -----------: | ----------: | ------: | --------: | ---------: | ---------: | ---------: | ----------: | ------------: | - | SystemDrawing | Jpg/baseline/jpeg420exif.jpg | 7.704 ms | 0.8684 ms | 0.0476 ms | 0.49 | 0.00 | - | - | - | 395 B | 0.15 | - | ImageSharp | Jpg/baseline/jpeg420exif.jpg | 9.357 ms | 0.3325 ms | 0.0182 ms | 0.60 | 0.00 | - | - | - | 16320 B | 6.18 | - | SkiaSharp | Jpg/baseline/jpeg420exif.jpg | 15.714 ms | 0.9678 ms | 0.0530 ms | 1.00 | 0.00 | - | - | - | 2640 B | 1.00 | - | | | | | | | | | | | | | - | SystemDrawing | Jpg/baseline/Lake.jpg | 12.424 ms | 0.4965 ms | 0.0272 ms | 1.73 | 0.01 | - | - | - | 399 B | 0.14 | - | ImageSharp | Jpg/baseline/Lake.jpg | 8.618 ms | 0.3989 ms | 0.0219 ms | 1.20 | 0.00 | - | - | - | 26033 B | 9.14 | - | SkiaSharp | Jpg/baseline/Lake.jpg | 7.162 ms | 0.3309 ms | 0.0181 ms | 1.00 | 0.00 | - | - | - | 2848 B | 1.00 | - | | | | | | | | | | | | | - | SystemDrawing | Jpg/icc-profiles/issue_2723.jpg | 13.057 ms | 1.2113 ms | 0.0664 ms | 0.70 | 0.00 | - | - | - | 399 B | 0.15 | - | ImageSharp | Jpg/icc-profiles/issue_2723.jpg | 20.457 ms | 2.8332 ms | 0.1553 ms | 1.09 | 0.01 | 968.7500 | 968.7500 | 968.7500 | 3631815 B | 1,375.69 | - | SkiaSharp | Jpg/icc-profiles/issue_2723.jpg | 18.783 ms | 2.0060 ms | 0.1100 ms | 1.00 | 0.01 | - | - | - | 2640 B | 1.00 | - | | | | | | | | | | | | | - | SystemDrawing | Jpg/icc-profiles/issue-129.jpg | 54.629 ms | 7.8786 ms | 0.4319 ms | 0.40 | 0.00 | - | - | - | 459 B | 0.17 | - | ImageSharp | Jpg/icc-profiles/issue-129.jpg | 155.386 ms | 26.1410 ms | 1.4329 ms | 1.13 | 0.01 | - | - | - | 3386594 B | 1,282.80 | - | SkiaSharp | Jpg/icc-profiles/issue-129.jpg | 137.003 ms | 7.6874 ms | 0.4214 ms | 1.00 | 0.00 | - | - | - | 2640 B | 1.00 | - | | | | | | | | | | | | | - | SystemDrawing | Jpg/icc-profiles/Momiji-AdobeRGB-yes.jpg | 5.180 ms | 2.6513 ms | 0.1453 ms | 2.60 | 0.07 | - | - | - | 395 B | 0.14 | - | ImageSharp | Jpg/icc-profiles/Momiji-AdobeRGB-yes.jpg | 2.034 ms | 0.1005 ms | 0.0055 ms | 1.02 | 0.01 | - | - | - | 26410 B | 9.27 | - | SkiaSharp | Jpg/icc-profiles/Momiji-AdobeRGB-yes.jpg | 1.990 ms | 0.3455 ms | 0.0189 ms | 1.00 | 0.01 | - | - | - | 2848 B | 1.00 | - | | | | | | | | | | | | | - | SystemDrawing | Jpg/issues/Issue518-Bad-RST-Progressive.jpg | 226.616 ms | 16.2417 ms | 0.8903 ms | 1.60 | 0.01 | - | - | - | 603 B | 0.23 | - | ImageSharp | Jpg/issues/Issue518-Bad-RST-Progressive.jpg | 143.563 ms | 36.5600 ms | 2.0040 ms | 1.01 | 0.01 | - | - | - | 47994 B | 18.18 | - | SkiaSharp | Jpg/issues/Issue518-Bad-RST-Progressive.jpg | 142.046 ms | 4.5723 ms | 0.2506 ms | 1.00 | 0.00 | - | - | - | 2640 B | 1.00 | + | Method | TestImage | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | + |-------------- |-------------------------------------------- |-----------:|-----------:|----------:|------:|--------:|----------:|----------:|----------:|----------:|------------:| + | SystemDrawing | Jpg/baseline/jpeg420exif.jpg | 7.794 ms | 0.9548 ms | 0.0523 ms | 0.53 | 0.00 | - | - | - | 331 B | 0.13 | + | ImageSharp | Jpg/baseline/jpeg420exif.jpg | 5.758 ms | 0.4221 ms | 0.0231 ms | 0.39 | 0.00 | 7.8125 | 7.8125 | 7.8125 | 18441 B | 7.13 | + | SkiaSharp | Jpg/baseline/jpeg420exif.jpg | 14.634 ms | 0.3240 ms | 0.0178 ms | 1.00 | 0.00 | - | - | - | 2585 B | 1.00 | + | | | | | | | | | | | | | + | SystemDrawing | Jpg/baseline/Lake.jpg | 12.413 ms | 0.3332 ms | 0.0183 ms | 1.64 | 0.00 | - | - | - | 335 B | 0.12 | + | ImageSharp | Jpg/baseline/Lake.jpg | 4.854 ms | 0.0921 ms | 0.0050 ms | 0.64 | 0.00 | - | - | - | 25198 B | 8.65 | + | SkiaSharp | Jpg/baseline/Lake.jpg | 7.583 ms | 0.1736 ms | 0.0095 ms | 1.00 | 0.00 | - | - | - | 2913 B | 1.00 | + | | | | | | | | | | | | | + | SystemDrawing | Jpg/icc-profiles/issue_2723.jpg | 13.112 ms | 1.5200 ms | 0.0833 ms | 0.66 | 0.00 | - | - | - | 335 B | 0.12 | + | ImageSharp | Jpg/icc-profiles/issue_2723.jpg | 16.552 ms | 2.4033 ms | 0.1317 ms | 0.83 | 0.01 | 937.5000 | 937.5000 | 937.5000 | 3646747 B | 1,275.53 | + | SkiaSharp | Jpg/icc-profiles/issue_2723.jpg | 19.904 ms | 0.3348 ms | 0.0184 ms | 1.00 | 0.00 | - | - | - | 2859 B | 1.00 | + | | | | | | | | | | | | | + | SystemDrawing | Jpg/icc-profiles/issue-129.jpg | 56.193 ms | 6.3019 ms | 0.3454 ms | 0.38 | 0.00 | - | - | - | 407 B | 0.14 | + | ImageSharp | Jpg/icc-profiles/issue-129.jpg | 126.909 ms | 18.9620 ms | 1.0394 ms | 0.85 | 0.01 | 200.0000 | 200.0000 | 200.0000 | 3430176 B | 1,206.11 | + | SkiaSharp | Jpg/icc-profiles/issue-129.jpg | 148.437 ms | 1.2990 ms | 0.0712 ms | 1.00 | 0.00 | - | - | - | 2844 B | 1.00 | + | | | | | | | | | | | | | + | SystemDrawing | Jpg/icc-profiles/Momiji-AdobeRGB-yes.jpg | 4.942 ms | 1.5762 ms | 0.0864 ms | 2.28 | 0.05 | - | - | - | 331 B | 0.12 | + | ImageSharp | Jpg/icc-profiles/Momiji-AdobeRGB-yes.jpg | 1.235 ms | 0.0711 ms | 0.0039 ms | 0.57 | 0.01 | - | - | - | 27445 B | 9.87 | + | SkiaSharp | Jpg/icc-profiles/Momiji-AdobeRGB-yes.jpg | 2.166 ms | 0.7905 ms | 0.0433 ms | 1.00 | 0.02 | - | - | - | 2780 B | 1.00 | + | | | | | | | | | | | | | + | SystemDrawing | Jpg/issues/Issue518-Bad-RST-Progressive.jpg | 221.317 ms | 1.7345 ms | 0.0951 ms | 1.44 | 0.01 | - | - | - | 539 B | 0.19 | + | ImageSharp | Jpg/issues/Issue518-Bad-RST-Progressive.jpg | 124.429 ms | 13.7442 ms | 0.7534 ms | 0.81 | 0.01 | 1000.0000 | 1000.0000 | 1000.0000 | 112146 B | 39.43 | + | SkiaSharp | Jpg/issues/Issue518-Bad-RST-Progressive.jpg | 153.279 ms | 32.1379 ms | 1.7616 ms | 1.00 | 0.01 | - | - | - | 2844 B | 1.00 | Allocated reports managed memory only. SystemDrawing returns native CMYK for issue-129.jpg. Its timing excludes the embedded CMYK-to-sRGB conversion diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/IdentifyJpeg.cs b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/IdentifyJpeg.cs index 9cc61c741d..348be91b75 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/IdentifyJpeg.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/IdentifyJpeg.cs @@ -13,18 +13,53 @@ public class IdentifyJpeg { private byte[] jpegBytes; + // Identify reads from this stream. Each call rewinds it, so the results measure the decoder only. + private MemoryStream inputStream; + private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); [Params(TestImages.Jpeg.Baseline.Jpeg420Exif, TestImages.Jpeg.Baseline.Calliphora)] public string TestImage { get; set; } [GlobalSetup] - public void ReadImages() => this.jpegBytes ??= File.ReadAllBytes(this.TestImageFullPath); + public void ReadImages() + { + this.jpegBytes ??= File.ReadAllBytes(this.TestImageFullPath); + this.inputStream ??= new MemoryStream(this.jpegBytes, false); + } + + /// + /// Releases the shared input stream. + /// + [GlobalCleanup] + public void Cleanup() + { + this.inputStream.Dispose(); + this.inputStream = null; + } [Benchmark] public ImageInfo Identify() { - using MemoryStream memoryStream = new(this.jpegBytes); - return JpegDecoder.Instance.Identify(DecoderOptions.Default, memoryStream); + this.inputStream.Position = 0; + return JpegDecoder.Instance.Identify(DecoderOptions.Default, this.inputStream); } + + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) + AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores + .NET SDK 11.0.100-preview.7.26381.103 + [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 + Job-RCQXAA : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 + + Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 + WarmupCount=3 + + | Method | TestImage | Mean | Error | StdDev | Gen0 | Gen1 | Allocated | + |--------- |----------------------------- |-----------:|-----------:|---------:|-------:|-------:|----------:| + | Identify | Jpg/baseline/Calliphora.jpg | 845.7 ns | 1,059.6 ns | 58.08 ns | 0.6514 | 0.0248 | 10.64 KB | + | Identify | Jpg/baseline/jpeg420exif.jpg | 5,527.5 ns | 1,496.7 ns | 82.04 ns | 1.7319 | 1.6327 | 28.39 KB | + + Allocated reports managed memory only. + */ } diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jxl/EncodeJxl.cs b/tests/ImageSharp.Benchmarks/Codecs/Jxl/EncodeJxl.cs index 8c26e0297b..1c7406de9b 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Jxl/EncodeJxl.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Jxl/EncodeJxl.cs @@ -7,8 +7,6 @@ using JxlNet; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Jxl; -using SixLabors.ImageSharp.IO; -using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Tests; using ManagedEncoder = SixLabors.ImageSharp.Formats.Jxl.JxlEncoder; @@ -22,7 +20,7 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jxl; /// /// Both encoders use the selected reconstruction distance and preserve transparent colors. /// The native reference uses effort two for progressive lossless output and effort one for the other settings. -/// Both timed methods include encoder creation, pooled output allocation, encoding and disposal. +/// Both timed methods include encoder creation, encoding and disposal. Both write into one shared output stream. /// [MarkdownExporter] [Config(typeof(Config.Short))] @@ -43,6 +41,12 @@ public unsafe class EncodeJxl /// private Image image; + /// + /// The output stream for both encoders. Each call rewinds it, so after the first call it has the full capacity, + /// and the results measure the encoders instead of the growth of a new stream. + /// + private MemoryStream outputStream; + /// /// Gets or sets the stored input decoded before benchmark measurement. /// @@ -86,6 +90,7 @@ public void ReadImages() // Both encoders read this same array. The image view has fresh metadata because conversion above produced sRGB pixels. // File decoding, color conversion, pixel packing and this one-time contiguous copy are outside measurement. this.image = Image.WrapMemory(this.pixels.AsMemory(), decoded.Width, decoded.Height); + this.outputStream = new MemoryStream(); uint version = NativeJxl.JxlEncoderVersion(); int effort = this.Distance == 0 && this.Progressive ? 2 : 1; @@ -95,44 +100,46 @@ public void ReadImages() } /// - /// Releases the source image view after measurement. Its managed pixel array remains owned by this benchmark instance. + /// Releases the source image view and the output stream after measurement. Its managed pixel array remains owned by this benchmark instance. /// [GlobalCleanup] - public void Cleanup() => this.image.Dispose(); + public void Cleanup() + { + this.image.Dispose(); + this.outputStream.Dispose(); + } /// - /// Creates a pooled output stream, encodes the prepared pixels and releases the output storage. + /// Encodes the prepared pixels into the shared output stream. /// /// The complete encoded length in bytes. [Benchmark] public long ImageSharp() { - MemoryAllocator allocator = this.image.Configuration.MemoryAllocator; - int chunkSize = (int)Math.Min((long)this.pixels.Length * Unsafe.SizeOf(), allocator.GetBufferCapacityInBytes()); - using ChunkedMemoryStream output = new(allocator, chunkSize); - this.image.Save(output, this.encoder); - return output.Length; + // The rewound stream keeps the bytes of the previous call, so the position after the write is the encoded length. + this.outputStream.Position = 0; + this.image.Save(this.outputStream, this.encoder); + return this.outputStream.Position; } /// - /// Creates the native encoder and the same pooled output stream, then releases both after encoding. + /// Creates the native encoder, encodes the prepared pixels into the shared output stream and releases the encoder. /// /// The complete encoded length in bytes. [Benchmark(Baseline = true)] public long Libjxl() { - MemoryAllocator allocator = this.image.Configuration.MemoryAllocator; - int chunkSize = (int)Math.Min((long)this.pixels.Length * Unsafe.SizeOf(), allocator.GetBufferCapacityInBytes()); - using ChunkedMemoryStream output = new(allocator, chunkSize); - this.EncodeLibjxl(output); - return output.Length; + // The rewound stream keeps the bytes of the previous call, so the position after the write is the encoded length. + this.outputStream.Position = 0; + this.EncodeLibjxl(this.outputStream); + return this.outputStream.Position; } /// /// Writes one raw codestream through the existing native bindings, with one thread and preserved hidden colors. /// /// The stream that receives complete encoded bytes. - private void EncodeLibjxl(ChunkedMemoryStream output) + private void EncodeLibjxl(MemoryStream output) { global::JxlNet.JxlEncoder* native = NativeJxl.JxlEncoderCreate(null); if (native is null) @@ -240,4 +247,67 @@ private static void Check(JxlEncoderStatus status) throw new InvalidOperationException($"libjxl rejected a benchmark encoder operation: {status}."); } } + + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) + AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores + .NET SDK 11.0.100-preview.7.26381.103 + [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 + Job-RCQXAA : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 + + Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 + WarmupCount=3 + + | Method | TestImage | Distance | Progressive | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | + |----------- |-------------------------- |--------- |------------ |-----------:|------------:|----------:|------:|--------:|---------:|---------:|---------:|----------:|------------:| + | ImageSharp | Jxl/dice.jxl | 0 | False | 4.416 ms | 0.5669 ms | 0.0311 ms | 0.52 | 0.00 | 7.8125 | 7.8125 | 7.8125 | 10959 B | 40.29 | + | Libjxl | Jxl/dice.jxl | 0 | False | 8.532 ms | 1.2719 ms | 0.0697 ms | 1.00 | 0.01 | - | - | - | 272 B | 1.00 | + | | | | | | | | | | | | | | | + | ImageSharp | Jxl/dice.jxl | 0 | True | 5.396 ms | 0.4826 ms | 0.0265 ms | 0.15 | 0.00 | 15.6250 | 15.6250 | 15.6250 | 21131 B | 28.91 | + | Libjxl | Jxl/dice.jxl | 0 | True | 36.439 ms | 2.0891 ms | 0.1145 ms | 1.00 | 0.00 | - | - | - | 731 B | 1.00 | + | | | | | | | | | | | | | | | + | ImageSharp | Jxl/dice.jxl | 1 | False | 5.157 ms | 1.3154 ms | 0.0721 ms | 0.24 | 0.00 | 15.6250 | 15.6250 | 15.6250 | 8770 B | 21.87 | + | Libjxl | Jxl/dice.jxl | 1 | False | 21.226 ms | 4.2897 ms | 0.2351 ms | 1.00 | 0.01 | - | - | - | 401 B | 1.00 | + | | | | | | | | | | | | | | | + | ImageSharp | Jxl/dice.jxl | 1 | True | 5.184 ms | 1.0561 ms | 0.0579 ms | 0.17 | 0.00 | 15.6250 | 15.6250 | 15.6250 | 8770 B | 21.87 | + | Libjxl | Jxl/dice.jxl | 1 | True | 29.779 ms | 1.8818 ms | 0.1031 ms | 1.00 | 0.00 | - | - | - | 401 B | 1.00 | + | | | | | | | | | | | | | | | + | ImageSharp | Jxl/has_permutation.jxl | 0 | False | 5.908 ms | 0.4359 ms | 0.0239 ms | 0.27 | 0.00 | 7.8125 | 7.8125 | 7.8125 | 11138 B | 27.78 | + | Libjxl | Jxl/has_permutation.jxl | 0 | False | 21.489 ms | 0.5220 ms | 0.0286 ms | 1.00 | 0.00 | - | - | - | 401 B | 1.00 | + | | | | | | | | | | | | | | | + | ImageSharp | Jxl/has_permutation.jxl | 0 | True | 13.321 ms | 0.8920 ms | 0.0489 ms | 0.10 | 0.00 | 31.2500 | 31.2500 | 31.2500 | 10451 B | 4.75 | + | Libjxl | Jxl/has_permutation.jxl | 0 | True | 139.785 ms | 10.9720 ms | 0.6014 ms | 1.00 | 0.01 | - | - | - | 2198 B | 1.00 | + | | | | | | | | | | | | | | | + | ImageSharp | Jxl/has_permutation.jxl | 1 | False | 21.953 ms | 9.0182 ms | 0.4943 ms | 0.29 | 0.01 | 62.5000 | 62.5000 | 62.5000 | 11102 B | 8.42 | + | Libjxl | Jxl/has_permutation.jxl | 1 | False | 75.391 ms | 9.5058 ms | 0.5210 ms | 1.00 | 0.01 | - | - | - | 1318 B | 1.00 | + | | | | | | | | | | | | | | | + | ImageSharp | Jxl/has_permutation.jxl | 1 | True | 30.069 ms | 20.8179 ms | 1.1411 ms | 0.23 | 0.02 | 62.5000 | 62.5000 | 62.5000 | 11135 B | 5.07 | + | Libjxl | Jxl/has_permutation.jxl | 1 | True | 132.523 ms | 163.2294 ms | 8.9472 ms | 1.00 | 0.08 | - | - | - | 2198 B | 1.00 | + | | | | | | | | | | | | | | | + | ImageSharp | Jxl/issue648_palette0.jxl | 0 | False | 15.571 ms | 2.1124 ms | 0.1158 ms | 0.41 | 0.01 | 31.2500 | 31.2500 | 31.2500 | 36222 B | 49.55 | + | Libjxl | Jxl/issue648_palette0.jxl | 0 | False | 37.653 ms | 21.5425 ms | 1.1808 ms | 1.00 | 0.04 | - | - | - | 731 B | 1.00 | + | | | | | | | | | | | | | | | + | ImageSharp | Jxl/issue648_palette0.jxl | 0 | True | 20.627 ms | 1.5929 ms | 0.0873 ms | 0.12 | 0.00 | 62.5000 | 62.5000 | 62.5000 | 12447 B | 5.66 | + | Libjxl | Jxl/issue648_palette0.jxl | 0 | True | 165.912 ms | 10.4955 ms | 0.5753 ms | 1.00 | 0.00 | - | - | - | 2198 B | 1.00 | + | | | | | | | | | | | | | | | + | ImageSharp | Jxl/issue648_palette0.jxl | 1 | False | 21.051 ms | 5.7083 ms | 0.3129 ms | 0.31 | 0.00 | 62.5000 | 62.5000 | 62.5000 | 10894 B | 9.30 | + | Libjxl | Jxl/issue648_palette0.jxl | 1 | False | 67.920 ms | 10.6730 ms | 0.5850 ms | 1.00 | 0.01 | - | - | - | 1171 B | 1.00 | + | | | | | | | | | | | | | | | + | ImageSharp | Jxl/issue648_palette0.jxl | 1 | True | 25.149 ms | 4.0404 ms | 0.2215 ms | 0.24 | 0.00 | 62.5000 | 62.5000 | 62.5000 | 10911 B | 6.11 | + | Libjxl | Jxl/issue648_palette0.jxl | 1 | True | 102.883 ms | 11.9190 ms | 0.6533 ms | 1.00 | 0.01 | - | - | - | 1787 B | 1.00 | + | | | | | | | | | | | | | | | + | ImageSharp | Jxl/progressive_ac.jxl | 0 | False | 29.919 ms | 2.5288 ms | 0.1386 ms | 0.39 | 0.00 | 31.2500 | 31.2500 | 31.2500 | 36222 B | 27.48 | + | Libjxl | Jxl/progressive_ac.jxl | 0 | False | 77.084 ms | 3.6761 ms | 0.2015 ms | 1.00 | 0.00 | - | - | - | 1318 B | 1.00 | + | | | | | | | | | | | | | | | + | ImageSharp | Jxl/progressive_ac.jxl | 0 | True | 40.029 ms | 1.0292 ms | 0.0564 ms | 0.15 | 0.00 | 153.8462 | 153.8462 | 153.8462 | 26330 B | 6.19 | + | Libjxl | Jxl/progressive_ac.jxl | 0 | True | 275.095 ms | 28.2466 ms | 1.5483 ms | 1.00 | 0.01 | - | - | - | 4252 B | 1.00 | + | | | | | | | | | | | | | | | + | ImageSharp | Jxl/progressive_ac.jxl | 1 | False | 41.247 ms | 29.3119 ms | 1.6067 ms | 0.27 | 0.01 | 153.8462 | 153.8462 | 153.8462 | 14381 B | 6.54 | + | Libjxl | Jxl/progressive_ac.jxl | 1 | False | 150.854 ms | 30.4876 ms | 1.6711 ms | 1.00 | 0.01 | - | - | - | 2198 B | 1.00 | + | | | | | | | | | | | | | | | + | ImageSharp | Jxl/progressive_ac.jxl | 1 | True | 53.636 ms | 5.6686 ms | 0.3107 ms | 0.27 | 0.00 | 222.2222 | 222.2222 | 222.2222 | 16764 B | 5.81 | + | Libjxl | Jxl/progressive_ac.jxl | 1 | True | 198.356 ms | 39.6413 ms | 2.1729 ms | 1.00 | 0.01 | - | - | - | 2883 B | 1.00 | + + Allocated reports managed memory only. + */ } diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jxl/README.md b/tests/ImageSharp.Benchmarks/Codecs/Jxl/README.md index eafb48d276..48ac88ac11 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Jxl/README.md +++ b/tests/ImageSharp.Benchmarks/Codecs/Jxl/README.md @@ -36,9 +36,8 @@ It measures distances zero and one, with progressive output enabled and disabled Setup decodes each stored input and prepares one unassociated sRGB RGBA8 array. Both encoders read that array. File I/O, source decoding and pixel preparation run outside measurement. Tests verify encoded pixels separately. -Each measured operation creates the encoder, writes complete output into a pooled stream, and releases both. -Both output streams use the RGBA8 input size as a minimum chunk-size hint, capped by the allocator and the stream's 4 MiB limit. -Chunk allocation remains inside measurement. The native pool can reserve more memory than a requested chunk's logical length. +Each measured operation creates the encoder, writes complete output into one shared `MemoryStream`, and releases the encoder. +Both encoders write into that stream. Each operation rewinds it, so the results measure the encoders instead of the growth of a new stream. Use the managed allocation column separately from total or native memory use. Both encoders preserve transparent colors and use one thread. Native effort is one except for progressive lossless output, which uses effort two. The effort-one fast lossless path always writes one pass and ignores the native `RESPONSIVE` option. diff --git a/tests/ImageSharp.Benchmarks/Codecs/Png/DecodePng.cs b/tests/ImageSharp.Benchmarks/Codecs/Png/DecodePng.cs index 3b01e600b6..12c7f8ccf4 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Png/DecodePng.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Png/DecodePng.cs @@ -14,6 +14,9 @@ public class DecodePng { private byte[] pngBytes; + // Both libraries read from this stream. Each call rewinds it, so the results measure the decoders only. + private MemoryStream inputStream; + private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); @@ -22,26 +25,39 @@ private string TestImageFullPath [GlobalSetup] public void ReadImages() - => this.pngBytes ??= File.ReadAllBytes(this.TestImageFullPath); + { + this.pngBytes ??= File.ReadAllBytes(this.TestImageFullPath); + this.inputStream ??= new MemoryStream(this.pngBytes, false); + } + + /// + /// Releases the shared input stream. + /// + [GlobalCleanup] + public void Cleanup() + { + this.inputStream.Dispose(); + this.inputStream = null; + } [Benchmark(Baseline = true, Description = "System.Drawing Png")] public SDSize PngSystemDrawing() { - using MemoryStream memoryStream = new(this.pngBytes); - using SDImage image = SDImage.FromStream(memoryStream); + this.inputStream.Position = 0; + using SDImage image = SDImage.FromStream(this.inputStream); return image.Size; } [Benchmark(Description = "ImageSharp Png")] public Size PngImageSharp() { - using MemoryStream memoryStream = new(this.pngBytes); - using Image image = Image.Load(memoryStream); + this.inputStream.Position = 0; + using Image image = Image.Load(this.inputStream); return image.Size; } - /* Results 28.10.2026 - BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9711) + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 11.0.100-preview.7.26381.103 [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 @@ -50,10 +66,10 @@ .NET SDK 11.0.100-preview.7.26381.103 Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 WarmupCount=3 - | Method | TestImage | Mean | Error | StdDev | Ratio | Allocated | Alloc Ratio | - | --------------------- | --------------- | ---------: | ----------: | ----------: | ------: | ----------: | ------------: | - | 'System.Drawing Png' | Png/splash.png | 1.259 ms | 0.1887 ms | 0.0103 ms | 1.00 | 393 B | 1.00 | - | 'ImageSharp Png' | Png/splash.png | 1.051 ms | 0.0469 ms | 0.0026 ms | 0.83 | 7776 B | 19.79 | + | Method | TestImage | Mean | Error | StdDev | Ratio | RatioSD | Allocated | Alloc Ratio | + |--------------------- |--------------- |---------:|----------:|----------:|------:|--------:|----------:|------------:| + | 'System.Drawing Png' | Png/splash.png | 1.378 ms | 1.0352 ms | 0.0567 ms | 1.00 | 0.05 | 329 B | 1.00 | + | 'ImageSharp Png' | Png/splash.png | 1.112 ms | 0.1993 ms | 0.0109 ms | 0.81 | 0.03 | 7744 B | 23.54 | Allocated reports managed memory only. */ diff --git a/tests/ImageSharp.Benchmarks/Codecs/Png/EncodeIndexedPng.cs b/tests/ImageSharp.Benchmarks/Codecs/Png/EncodeIndexedPng.cs index 69779731be..68e8fd0367 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Png/EncodeIndexedPng.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Png/EncodeIndexedPng.cs @@ -21,6 +21,10 @@ public class EncodeIndexedPng private FileStream bmpStream; private Image bmpCore; + // All methods write into this stream. Each call rewinds it, so after the first call it has the full capacity, + // and the results measure the encoders instead of the growth of a new stream. + private MemoryStream outputStream; + [GlobalSetup] public void ReadImages() { @@ -29,6 +33,7 @@ public void ReadImages() this.bmpStream = File.OpenRead(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, TestImages.Bmp.Car)); this.bmpCore = Image.Load(this.bmpStream); this.bmpStream.Position = 0; + this.outputStream = new MemoryStream(); } } @@ -38,53 +43,77 @@ public void Cleanup() this.bmpStream.Dispose(); this.bmpStream = null; this.bmpCore.Dispose(); + this.outputStream.Dispose(); } [Benchmark(Baseline = true, Description = "ImageSharp Hexadecatree Png")] public void PngCoreHexadecatree() { - using MemoryStream memoryStream = new(); + this.outputStream.Position = 0; PngEncoder options = new() { Quantizer = KnownQuantizers.Hexadecatree }; - this.bmpCore.SaveAsPng(memoryStream, options); + this.bmpCore.SaveAsPng(this.outputStream, options); } [Benchmark(Description = "ImageSharp Hexadecatree NoDither Png")] public void PngCoreHexadecatreeNoDither() { - using MemoryStream memoryStream = new(); + this.outputStream.Position = 0; PngEncoder options = new() { Quantizer = new HexadecatreeQuantizer(new QuantizerOptions { Dither = null }) }; - this.bmpCore.SaveAsPng(memoryStream, options); + this.bmpCore.SaveAsPng(this.outputStream, options); } [Benchmark(Description = "ImageSharp Palette Png")] public void PngCorePalette() { - using MemoryStream memoryStream = new(); + this.outputStream.Position = 0; PngEncoder options = new() { Quantizer = KnownQuantizers.WebSafe }; - this.bmpCore.SaveAsPng(memoryStream, options); + this.bmpCore.SaveAsPng(this.outputStream, options); } [Benchmark(Description = "ImageSharp Palette NoDither Png")] public void PngCorePaletteNoDither() { - using MemoryStream memoryStream = new(); + this.outputStream.Position = 0; PngEncoder options = new() { Quantizer = new WebSafePaletteQuantizer(new QuantizerOptions { Dither = null }) }; - this.bmpCore.SaveAsPng(memoryStream, options); + this.bmpCore.SaveAsPng(this.outputStream, options); } [Benchmark(Description = "ImageSharp Wu Png")] public void PngCoreWu() { - using MemoryStream memoryStream = new(); + this.outputStream.Position = 0; PngEncoder options = new() { Quantizer = KnownQuantizers.Wu }; - this.bmpCore.SaveAsPng(memoryStream, options); + this.bmpCore.SaveAsPng(this.outputStream, options); } [Benchmark(Description = "ImageSharp Wu NoDither Png")] public void PngCoreWuNoDither() { - using MemoryStream memoryStream = new(); + this.outputStream.Position = 0; PngEncoder options = new() { Quantizer = new WuQuantizer(new QuantizerOptions { Dither = null }), ColorType = PngColorType.Palette }; - this.bmpCore.SaveAsPng(memoryStream, options); + this.bmpCore.SaveAsPng(this.outputStream, options); } + + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) + AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores + .NET SDK 11.0.100-preview.7.26381.103 + [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 + Job-RCQXAA : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 + + Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 + WarmupCount=3 + + | Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | + |--------------------------------------- |----------:|----------:|----------:|------:|--------:|----------:|----------:|----------:|----------:|------------:| + | 'ImageSharp Hexadecatree Png' | 8.073 ms | 2.5664 ms | 0.1407 ms | 1.00 | 0.02 | - | - | - | 2.24 KB | 1.00 | + | 'ImageSharp Hexadecatree NoDither Png' | 8.125 ms | 1.9564 ms | 0.1072 ms | 1.01 | 0.02 | - | - | - | 2.31 KB | 1.03 | + | 'ImageSharp Palette Png' | 8.058 ms | 1.4806 ms | 0.0812 ms | 1.00 | 0.02 | - | - | - | 2.24 KB | 1.00 | + | 'ImageSharp Palette NoDither Png' | 7.986 ms | 0.7053 ms | 0.0387 ms | 0.99 | 0.02 | - | - | - | 2.37 KB | 1.06 | + | 'ImageSharp Wu Png' | 8.039 ms | 1.1671 ms | 0.0640 ms | 1.00 | 0.02 | - | - | - | 2.24 KB | 1.00 | + | 'ImageSharp Wu NoDither Png' | 29.626 ms | 8.9982 ms | 0.4932 ms | 3.67 | 0.08 | 1937.5000 | 1937.5000 | 1937.5000 | 584.75 KB | 260.68 | + + Allocated reports managed memory only. + Input: Bmp/Car.bmp. + */ } diff --git a/tests/ImageSharp.Benchmarks/Codecs/Png/EncodePng.cs b/tests/ImageSharp.Benchmarks/Codecs/Png/EncodePng.cs index b8de50a9ca..1dae768bea 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Png/EncodePng.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Png/EncodePng.cs @@ -18,6 +18,10 @@ public class EncodePng private SDImage bmpDrawing; private Image bmpCore; + // Both libraries write into this stream. Each call rewinds it, so after the first call it has the full capacity, + // and the results measure the encoders instead of the growth of a new stream. + private MemoryStream outputStream; + [Params(false)] public bool LargeImage { get; set; } @@ -34,6 +38,7 @@ public void ReadImages() this.bmpCore = Image.Load(this.bmpStream); this.bmpStream.Position = 0; this.bmpDrawing = SDImage.FromStream(this.bmpStream); + this.outputStream = new MemoryStream(); } } @@ -44,25 +49,26 @@ public void Cleanup() this.bmpStream = null; this.bmpCore.Dispose(); this.bmpDrawing.Dispose(); + this.outputStream.Dispose(); } [Benchmark(Baseline = true, Description = "System.Drawing Png")] public void PngSystemDrawing() { - using MemoryStream memoryStream = new(); - this.bmpDrawing.Save(memoryStream, ImageFormat.Png); + this.outputStream.Position = 0; + this.bmpDrawing.Save(this.outputStream, ImageFormat.Png); } [Benchmark(Description = "ImageSharp Png")] public void PngCore() { - using MemoryStream memoryStream = new(); + this.outputStream.Position = 0; PngEncoder encoder = new() { FilterMethod = PngFilterMethod.None }; - this.bmpCore.SaveAsPng(memoryStream, encoder); + this.bmpCore.SaveAsPng(this.outputStream, encoder); } - /* Results 28.10.2026 - BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9711) + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 11.0.100-preview.7.26381.103 [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 @@ -71,10 +77,10 @@ .NET SDK 11.0.100-preview.7.26381.103 Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 WarmupCount=3 - | Method | LargeImage | Mean | Error | StdDev | Ratio | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | - | --------------------- | ----------- | ---------: | ----------: | ----------: | ------: | ---------: | ---------: | ---------: | ----------: | ------------: | - | 'System.Drawing Png' | False | 6.545 ms | 0.4520 ms | 0.0248 ms | 1.00 | 281.2500 | 281.2500 | 281.2500 | 963.73 KB | 1.00 | - | 'ImageSharp Png' | False | 6.930 ms | 0.8409 ms | 0.0461 ms | 1.06 | 281.2500 | 281.2500 | 281.2500 | 963.26 KB | 1.00 | + | Method | LargeImage | Mean | Error | StdDev | Ratio | RatioSD | Allocated | Alloc Ratio | + |--------------------- |----------- |---------:|----------:|----------:|------:|--------:|----------:|------------:| + | 'System.Drawing Png' | False | 6.728 ms | 4.7442 ms | 0.2600 ms | 1.00 | 0.05 | 3.18 KB | 1.00 | + | 'ImageSharp Png' | False | 6.897 ms | 0.2821 ms | 0.0155 ms | 1.03 | 0.03 | 2.24 KB | 0.70 | Allocated reports managed memory only. LargeImage=False selects Bmp/Car.bmp. ImageSharp uses PngFilterMethod.None. diff --git a/tests/ImageSharp.Benchmarks/Codecs/Tga/DecodeTga.cs b/tests/ImageSharp.Benchmarks/Codecs/Tga/DecodeTga.cs index da0d1676cc..c300cd4f8e 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Tga/DecodeTga.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Tga/DecodeTga.cs @@ -19,25 +19,44 @@ public class DecodeTga private byte[] data; + // ImageMagick and ImageSharp read from this stream. Each call rewinds it, so the results measure the decoders only. + // Pfim reads the byte array. + private MemoryStream inputStream; + [Params(TestImages.Tga.Bit24BottomLeft)] public string TestImage { get; set; } [GlobalSetup] public void SetupData() - => this.data = File.ReadAllBytes(this.TestImageFullPath); + { + this.data = File.ReadAllBytes(this.TestImageFullPath); + this.inputStream = new MemoryStream(this.data, false); + } + + /// + /// Releases the shared input stream. + /// + [GlobalCleanup] + public void Cleanup() + { + this.inputStream.Dispose(); + this.inputStream = null; + } [Benchmark(Baseline = true, Description = "ImageMagick Tga")] public uint TgaImageMagick() { MagickReadSettings settings = new() { Format = MagickFormat.Tga }; - using MagickImage image = new(new MemoryStream(this.data), settings); + this.inputStream.Position = 0; + using MagickImage image = new(this.inputStream, settings); return image.Width; } [Benchmark(Description = "ImageSharp Tga")] public int TgaImageSharp() { - using Image image = Image.Load(this.data); + this.inputStream.Position = 0; + using Image image = Image.Load(this.inputStream); return image.Width; } @@ -64,8 +83,8 @@ public void Return(byte[] data) public int Rented => this.rented; } - /* Results 28.10.2026 - BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9711) + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 11.0.100-preview.7.26381.103 [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 @@ -74,11 +93,11 @@ .NET SDK 11.0.100-preview.7.26381.103 Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 WarmupCount=3 - | Method | TestImage | Mean | Error | StdDev | Ratio | Gen0 | Allocated | Alloc Ratio | - | ------------------ | -------------------- | -----------: | -----------: | ----------: | ------: | -------: | ----------: | ------------: | - | 'ImageMagick Tga' | Tga/targa_24bit.tga | 631.777 μs | 25.8058 μs | 1.4145 μs | 1.000 | - | 4805 B | 1.00 | - | 'ImageSharp Tga' | Tga/targa_24bit.tga | 14.277 μs | 1.3643 μs | 0.0748 μs | 0.023 | 0.1221 | 2114 B | 0.44 | - | 'Pfim Tga' | Tga/targa_24bit.tga | 1.319 μs | 0.1189 μs | 0.0065 μs | 0.002 | 0.0134 | 248 B | 0.05 | + | Method | TestImage | Mean | Error | StdDev | Ratio | Gen0 | Allocated | Alloc Ratio | + |------------------ |-------------------- |-----------:|------------:|----------:|------:|-------:|----------:|------------:| + | 'ImageMagick Tga' | Tga/targa_24bit.tga | 678.098 us | 118.5345 us | 6.4973 us | 1.000 | - | 4741 B | 1.00 | + | 'ImageSharp Tga' | Tga/targa_24bit.tga | 14.749 us | 2.3542 us | 0.1290 us | 0.022 | 0.1221 | 2058 B | 0.43 | + | 'Pfim Tga' | Tga/targa_24bit.tga | 1.322 us | 0.2019 us | 0.0111 us | 0.002 | 0.0134 | 248 B | 0.05 | Allocated reports managed memory only. */ diff --git a/tests/ImageSharp.Benchmarks/Codecs/Tga/EncodeTga.cs b/tests/ImageSharp.Benchmarks/Codecs/Tga/EncodeTga.cs index 784a0e6177..a4c940b02b 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Tga/EncodeTga.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Tga/EncodeTga.cs @@ -14,6 +14,10 @@ public class EncodeTga private MagickImage tgaMagick; private Image tga; + // Both libraries write into this stream. Each call rewinds it, so after the first call it has the full capacity, + // and the results measure the encoders instead of the growth of a new stream. + private MemoryStream outputStream; + private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); @@ -27,6 +31,7 @@ public void ReadImages() { this.tga = Image.Load(this.TestImageFullPath); this.tgaMagick = new MagickImage(this.TestImageFullPath); + this.outputStream = new MemoryStream(); } } @@ -36,24 +41,25 @@ public void Cleanup() this.tga.Dispose(); this.tga = null; this.tgaMagick.Dispose(); + this.outputStream.Dispose(); } [Benchmark(Baseline = true, Description = "Magick Tga")] public void MagickTga() { - using MemoryStream memoryStream = new(); - this.tgaMagick.Write(memoryStream, MagickFormat.Tga); + this.outputStream.Position = 0; + this.tgaMagick.Write(this.outputStream, MagickFormat.Tga); } [Benchmark(Description = "ImageSharp Tga")] public void ImageSharpTga() { - using MemoryStream memoryStream = new(); - this.tga.SaveAsTga(memoryStream); + this.outputStream.Position = 0; + this.tga.SaveAsTga(this.outputStream); } - /* Results 28.10.2026 - BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9711) + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 11.0.100-preview.7.26381.103 [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 @@ -62,10 +68,10 @@ .NET SDK 11.0.100-preview.7.26381.103 Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 WarmupCount=3 - | Method | TestImage | Mean | Error | StdDev | Ratio | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | - | ----------------- | -------------------- | ------------: | ---------: | ---------: | ------: | --------: | --------: | --------: | ----------: | ------------: | - | 'Magick Tga' | Tga/targa_24bit.tga | 1,163.39 μs | 10.06 μs | 0.552 μs | 1.00 | 41.0156 | 41.0156 | 41.0156 | 257.63 KB | 1.00 | - | 'ImageSharp Tga' | Tga/targa_24bit.tga | 77.12 μs | 12.16 μs | 0.667 μs | 0.07 | 7.8125 | 1.4648 | - | 128.27 KB | 0.50 | + | Method | TestImage | Mean | Error | StdDev | Ratio | Allocated | Alloc Ratio | + |----------------- |-------------------- |------------:|---------:|---------:|------:|----------:|------------:| + | 'Magick Tga' | Tga/targa_24bit.tga | 1,139.82 us | 77.89 us | 4.269 us | 1.00 | 1050 B | 1.00 | + | 'ImageSharp Tga' | Tga/targa_24bit.tga | 62.50 us | 16.63 us | 0.912 us | 0.05 | 256 B | 0.24 | Allocated reports managed memory only. */ diff --git a/tests/ImageSharp.Benchmarks/Codecs/Tiff/DecodeTiff.cs b/tests/ImageSharp.Benchmarks/Codecs/Tiff/DecodeTiff.cs index 34590890da..c3cb5b2f2c 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Tiff/DecodeTiff.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Tiff/DecodeTiff.cs @@ -21,6 +21,9 @@ public class DecodeTiff { private byte[] data; + // All three libraries read from this stream. Each call rewinds it, so the results measure the decoders only. + private MemoryStream inputStream; + #if BIG_TESTS private static readonly int BufferSize = 1024 * 68; @@ -65,13 +68,24 @@ public void ReadImages() { // File I/O belongs to setup so measured iterations include only decoding. this.data = File.ReadAllBytes(this.TestImageFullPath); + this.inputStream = new MemoryStream(this.data, false); + } + + /// + /// Releases the shared input stream. + /// + [GlobalCleanup] + public void Cleanup() + { + this.inputStream.Dispose(); + this.inputStream = null; } [Benchmark(Baseline = true, Description = "System.Drawing Tiff")] public SDSize TiffSystemDrawing() { - using MemoryStream memoryStream = new(this.data); - using SDImage image = SDImage.FromStream(memoryStream); + this.inputStream.Position = 0; + using SDImage image = SDImage.FromStream(this.inputStream); return image.Size; } @@ -83,21 +97,21 @@ public uint TiffImageMagick() { // Constructing MagickImage reads the pixels. Ping would read only the headers and skew this comparison. MagickReadSettings settings = new() { Format = MagickFormat.Tiff }; - using MemoryStream memoryStream = new(this.data); - using MagickImage image = new(memoryStream, settings); + this.inputStream.Position = 0; + using MagickImage image = new(this.inputStream, settings); return image.Width; } [Benchmark(Description = "ImageSharp Tiff")] public Size TiffCore() { - using MemoryStream ms = new(this.data); - using Image image = Image.Load(ms); + this.inputStream.Position = 0; + using Image image = Image.Load(this.inputStream); return image.Size; } - /* Results 28.10.2026 - BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9711) + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 11.0.100-preview.7.26381.103 [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 @@ -106,43 +120,43 @@ .NET SDK 11.0.100-preview.7.26381.103 Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 WarmupCount=3 - | Method | TestImage | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | - | ---------------------- | ----------------------------------------------- | -------------: | -----------: | ----------: | ------: | --------: | ----------: | ---------: | --------: | -----------: | ------------: | - | 'System.Drawing Tiff' | Tiff/Calliphora_grayscale_uncompressed.tiff | 45.70 μs | 3.994 μs | 0.219 μs | 1.00 | 0.01 | - | - | - | 392 B | 1.00 | - | 'ImageMagick Tiff' | Tiff/Calliphora_grayscale_uncompressed.tiff | 101.78 μs | 18.275 μs | 1.002 μs | 2.23 | 0.02 | 0.2441 | - | - | 4797 B | 12.24 | - | 'ImageSharp Tiff' | Tiff/Calliphora_grayscale_uncompressed.tiff | 31.80 μs | 0.390 μs | 0.021 μs | 0.70 | 0.00 | 0.4883 | - | - | 8896 B | 22.69 | - | | | | | | | | | | | | | - | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_deflate_predictor.tiff | 463.94 μs | 104.494 μs | 5.728 μs | 1.00 | 0.02 | - | - | - | 392 B | 1.00 | - | 'ImageMagick Tiff' | Tiff/Calliphora_rgb_deflate_predictor.tiff | 745.63 μs | 106.212 μs | 5.822 μs | 1.61 | 0.02 | - | - | - | 4805 B | 12.26 | - | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_deflate_predictor.tiff | 422.26 μs | 48.053 μs | 2.634 μs | 0.91 | 0.01 | 0.4883 | - | - | 9376 B | 23.92 | - | | | | | | | | | | | | | - | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_lzw_predictor.tiff | 637.96 μs | 130.474 μs | 7.152 μs | 1.00 | 0.01 | - | - | - | 392 B | 1.00 | - | 'ImageMagick Tiff' | Tiff/Calliphora_rgb_lzw_predictor.tiff | 626.72 μs | 69.555 μs | 3.813 μs | 0.98 | 0.01 | - | - | - | 4805 B | 12.26 | - | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_lzw_predictor.tiff | 1,408.41 μs | 108.544 μs | 5.950 μs | 2.21 | 0.02 | 173.8281 | 85.9375 | - | 2921573 B | 7,452.99 | - | | | | | | | | | | | | | - | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_packbits.tiff | 119.91 μs | 4.718 μs | 0.259 μs | 1.00 | 0.00 | - | - | - | 392 B | 1.00 | - | 'ImageMagick Tiff' | Tiff/Calliphora_rgb_packbits.tiff | 160.04 μs | 49.690 μs | 2.724 μs | 1.33 | 0.02 | 0.2441 | - | - | 4798 B | 12.24 | - | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_packbits.tiff | 17.28 μs | 0.713 μs | 0.039 μs | 0.14 | 0.00 | 0.5188 | - | - | 8928 B | 22.78 | - | | | | | | | | | | | | | - | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_palette_lzw_predictor.tiff | 6,385.73 μs | 610.637 μs | 33.471 μs | 1.00 | 0.01 | - | - | - | 395 B | 1.00 | - | 'ImageMagick Tiff' | Tiff/Calliphora_rgb_palette_lzw_predictor.tiff | 8,851.98 μs | 703.671 μs | 38.571 μs | 1.39 | 0.01 | - | - | - | 4994 B | 12.64 | - | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_palette_lzw_predictor.tiff | 14,182.08 μs | 632.607 μs | 34.675 μs | 2.22 | 0.01 | 1078.1250 | 531.2500 | 15.6250 | 18027055 B | 45,638.11 | - | | | | | | | | | | | | | - | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | 113.54 μs | 10.179 μs | 0.558 μs | 1.00 | 0.01 | - | - | - | 392 B | 1.00 | - | 'ImageMagick Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | 136.52 μs | 5.909 μs | 0.324 μs | 1.20 | 0.01 | 0.2441 | - | - | 4798 B | 12.24 | - | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | 11.31 μs | 0.137 μs | 0.007 μs | 0.10 | 0.00 | 0.5341 | - | - | 8944 B | 22.82 | - | | | | | | | | | | | | | - | 'System.Drawing Tiff' | Tiff/ccitt_fax3_all_terminating_codes.tiff | 22.19 μs | 2.117 μs | 0.116 μs | 1.00 | 0.01 | - | - | - | 392 B | 1.00 | - | 'ImageMagick Tiff' | Tiff/ccitt_fax3_all_terminating_codes.tiff | 65.87 μs | 9.096 μs | 0.499 μs | 2.97 | 0.02 | 0.2441 | - | - | 4797 B | 12.24 | - | 'ImageSharp Tiff' | Tiff/ccitt_fax3_all_terminating_codes.tiff | 31.82 μs | 0.088 μs | 0.005 μs | 1.43 | 0.01 | 0.4272 | - | - | 7224 B | 18.43 | - | | | | | | | | | | | | | - | 'System.Drawing Tiff' | Tiff/CCITTGroup4.tiff | 727.30 μs | 35.418 μs | 1.941 μs | 1.00 | 0.00 | - | - | - | 392 B | 1.00 | - | 'ImageMagick Tiff' | Tiff/CCITTGroup4.tiff | 4,232.42 μs | 693.868 μs | 38.033 μs | 5.82 | 0.05 | - | - | - | 4893 B | 12.48 | - | 'ImageSharp Tiff' | Tiff/CCITTGroup4.tiff | 10,706.91 μs | 310.112 μs | 16.998 μs | 14.72 | 0.04 | 15.6250 | 15.6250 | 15.6250 | 8200 B | 20.92 | - | | | | | | | | | | | | | - | 'System.Drawing Tiff' | Tiff/huffman_rle_all_makeup_codes.tiff | 27.24 μs | 4.481 μs | 0.246 μs | 1.00 | 0.01 | - | - | - | 392 B | 1.00 | - | 'ImageMagick Tiff' | Tiff/huffman_rle_all_makeup_codes.tiff | 137.86 μs | 8.790 μs | 0.482 μs | 5.06 | 0.04 | 0.2441 | - | - | 4798 B | 12.24 | - | 'ImageSharp Tiff' | Tiff/huffman_rle_all_makeup_codes.tiff | 562.16 μs | 12.596 μs | 0.690 μs | 20.64 | 0.16 | - | - | - | 7800 B | 19.90 | + | Method | TestImage | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | + |---------------------- |----------------------------------------------- |-------------:|-------------:|-----------:|------:|--------:|----------:|---------:|--------:|-----------:|------------:| + | 'System.Drawing Tiff' | Tiff/Calliphora_grayscale_uncompressed.tiff | 44.76 us | 26.782 us | 1.468 us | 1.00 | 0.04 | - | - | - | 328 B | 1.00 | + | 'ImageMagick Tiff' | Tiff/Calliphora_grayscale_uncompressed.tiff | 103.15 us | 81.113 us | 4.446 us | 2.31 | 0.11 | 0.2441 | - | - | 4733 B | 14.43 | + | 'ImageSharp Tiff' | Tiff/Calliphora_grayscale_uncompressed.tiff | 32.26 us | 2.838 us | 0.156 us | 0.72 | 0.02 | 0.4883 | - | - | 8885 B | 27.09 | + | | | | | | | | | | | | | + | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_deflate_predictor.tiff | 470.78 us | 198.241 us | 10.866 us | 1.00 | 0.03 | - | - | - | 328 B | 1.00 | + | 'ImageMagick Tiff' | Tiff/Calliphora_rgb_deflate_predictor.tiff | 757.61 us | 174.943 us | 9.589 us | 1.61 | 0.04 | - | - | - | 4741 B | 14.45 | + | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_deflate_predictor.tiff | 423.02 us | 49.025 us | 2.687 us | 0.90 | 0.02 | 0.4883 | - | - | 9476 B | 28.89 | + | | | | | | | | | | | | | + | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_lzw_predictor.tiff | 701.51 us | 16.524 us | 0.906 us | 1.00 | 0.00 | - | - | - | 328 B | 1.00 | + | 'ImageMagick Tiff' | Tiff/Calliphora_rgb_lzw_predictor.tiff | 600.04 us | 22.602 us | 1.239 us | 0.86 | 0.00 | - | - | - | 4741 B | 14.45 | + | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_lzw_predictor.tiff | 1,483.78 us | 908.774 us | 49.813 us | 2.12 | 0.06 | 173.8281 | 85.9375 | - | 2922070 B | 8,908.75 | + | | | | | | | | | | | | | + | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_packbits.tiff | 121.73 us | 30.683 us | 1.682 us | 1.00 | 0.02 | - | - | - | 328 B | 1.00 | + | 'ImageMagick Tiff' | Tiff/Calliphora_rgb_packbits.tiff | 168.06 us | 124.119 us | 6.803 us | 1.38 | 0.05 | 0.2441 | - | - | 4734 B | 14.43 | + | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_packbits.tiff | 17.84 us | 3.610 us | 0.198 us | 0.15 | 0.00 | 0.5188 | - | - | 9064 B | 27.63 | + | | | | | | | | | | | | | + | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_palette_lzw_predictor.tiff | 6,765.49 us | 925.739 us | 50.743 us | 1.00 | 0.01 | - | - | - | 331 B | 1.00 | + | 'ImageMagick Tiff' | Tiff/Calliphora_rgb_palette_lzw_predictor.tiff | 8,907.99 us | 840.021 us | 46.044 us | 1.32 | 0.01 | - | - | - | 4934 B | 14.91 | + | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_palette_lzw_predictor.tiff | 13,774.03 us | 955.519 us | 52.375 us | 2.04 | 0.01 | 1078.1250 | 531.2500 | 15.6250 | 18043481 B | 54,512.03 | + | | | | | | | | | | | | | + | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | 112.34 us | 17.190 us | 0.942 us | 1.00 | 0.01 | - | - | - | 328 B | 1.00 | + | 'ImageMagick Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | 137.91 us | 24.123 us | 1.322 us | 1.23 | 0.01 | 0.2441 | - | - | 4734 B | 14.43 | + | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | 11.52 us | 1.577 us | 0.086 us | 0.10 | 0.00 | 0.5188 | - | - | 8868 B | 27.04 | + | | | | | | | | | | | | | + | 'System.Drawing Tiff' | Tiff/ccitt_fax3_all_terminating_codes.tiff | 22.27 us | 5.360 us | 0.294 us | 1.00 | 0.02 | - | - | - | 328 B | 1.00 | + | 'ImageMagick Tiff' | Tiff/ccitt_fax3_all_terminating_codes.tiff | 66.43 us | 3.545 us | 0.194 us | 2.98 | 0.03 | 0.2441 | - | - | 4733 B | 14.43 | + | 'ImageSharp Tiff' | Tiff/ccitt_fax3_all_terminating_codes.tiff | 32.31 us | 7.871 us | 0.431 us | 1.45 | 0.02 | 0.4272 | - | - | 7195 B | 21.94 | + | | | | | | | | | | | | | + | 'System.Drawing Tiff' | Tiff/CCITTGroup4.tiff | 758.06 us | 383.972 us | 21.047 us | 1.00 | 0.03 | - | - | - | 328 B | 1.00 | + | 'ImageMagick Tiff' | Tiff/CCITTGroup4.tiff | 4,364.34 us | 594.351 us | 32.578 us | 5.76 | 0.14 | - | - | - | 4831 B | 14.73 | + | 'ImageSharp Tiff' | Tiff/CCITTGroup4.tiff | 10,130.63 us | 3,936.036 us | 215.747 us | 13.37 | 0.40 | 15.6250 | 15.6250 | 15.6250 | 16556 B | 50.48 | + | | | | | | | | | | | | | + | 'System.Drawing Tiff' | Tiff/huffman_rle_all_makeup_codes.tiff | 32.13 us | 19.569 us | 1.073 us | 1.00 | 0.04 | - | - | - | 328 B | 1.00 | + | 'ImageMagick Tiff' | Tiff/huffman_rle_all_makeup_codes.tiff | 216.88 us | 535.164 us | 29.334 us | 6.75 | 0.81 | 0.2441 | - | - | 4734 B | 14.43 | + | 'ImageSharp Tiff' | Tiff/huffman_rle_all_makeup_codes.tiff | 651.87 us | 592.187 us | 32.460 us | 20.30 | 1.05 | - | - | - | 8760 B | 26.71 | Allocated reports managed memory only. */ diff --git a/tests/ImageSharp.Benchmarks/Codecs/Tiff/DecodeTiffIcc.cs b/tests/ImageSharp.Benchmarks/Codecs/Tiff/DecodeTiffIcc.cs index eecb76490f..8a060ca3f8 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Tiff/DecodeTiffIcc.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Tiff/DecodeTiffIcc.cs @@ -21,6 +21,9 @@ public class DecodeTiffIcc private ImageMagick.ColorProfile targetProfile; private RenderingIntent renderingIntent; + // Both libraries read from this stream. Each call rewinds it, so the results measure the decoders only. + private MemoryStream inputStream; + /// /// Gets or sets the real TIFF supplying encoded samples and an embedded ICC profile. /// @@ -56,6 +59,17 @@ public void Setup() }; this.targetProfile = new ImageMagick.ColorProfile(CompactSrgbV4Profile.Profile.ToByteArray()); + this.inputStream = new MemoryStream(this.data, false); + } + + /// + /// Releases the shared input stream. + /// + [GlobalCleanup] + public void Cleanup() + { + this.inputStream.Dispose(); + this.inputStream = null; } /// @@ -65,8 +79,8 @@ public void Setup() [Benchmark(Baseline = true)] public uint ImageMagick() { - using MemoryStream source = new(this.data); - using MagickImage image = new(source, new MagickReadSettings { Format = MagickFormat.Tiff }); + this.inputStream.Position = 0; + using MagickImage image = new(this.inputStream, new MagickReadSettings { Format = MagickFormat.Tiff }); image.RenderingIntent = this.renderingIntent; image.BlackPointCompensation = false; @@ -86,15 +100,15 @@ public uint ImageMagick() [Benchmark] public Size ImageSharp() { - using MemoryStream source = new(this.data); + this.inputStream.Position = 0; using Image image = Image.Load( new DecoderOptions(Configuration.Default) { ColorProfileHandling = ColorProfileHandling.Preserve }, - source); + this.inputStream); return image.Size; } - /* Results 28.10.2026 - BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9711) + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 11.0.100-preview.7.26381.103 [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 @@ -103,22 +117,22 @@ .NET SDK 11.0.100-preview.7.26381.103 Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 WarmupCount=3 - | Method | TestImage | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | - | ------------ | -------------------------------------------- | -------------: | -------------: | ----------: | ------: | --------: | ----------: | ---------: | ---------: | -----------: | ------------: | - | ImageMagick | Tiff/icc-profiles/Perceptual_CIELAB.tiff | 1,141.99 μs | 288.296 μs | 15.802 μs | 1.00 | 0.02 | - | - | - | 6.38 KB | 1.00 | - | ImageSharp | Tiff/icc-profiles/Perceptual_CIELAB.tiff | 157.66 μs | 3.944 μs | 0.216 μs | 0.14 | 0.00 | 1.7090 | - | - | 29.71 KB | 4.65 | - | | | | | | | | | | | | | - | ImageMagick | Tiff/icc-profiles/Perceptual_CMYK.tiff | 56,929.37 μs | 1,550.725 μs | 85.000 μs | 1.000 | 0.00 | - | - | - | 7.76 KB | 1.00 | - | ImageSharp | Tiff/icc-profiles/Perceptual_CMYK.tiff | 423.49 μs | 223.138 μs | 12.231 μs | 0.007 | 0.00 | 1000.0000 | 983.3984 | 977.5391 | 4944.71 KB | 636.98 | - | | | | | | | | | | | | | - | ImageMagick | Tiff/icc-profiles/Perceptual_RGB16.tiff | 16,853.11 μs | 1,142.062 μs | 62.600 μs | 1.000 | 0.00 | - | - | - | 6.75 KB | 1.00 | - | ImageSharp | Tiff/icc-profiles/Perceptual_RGB16.tiff | 92.18 μs | 16.730 μs | 0.917 μs | 0.005 | 0.00 | 1.9531 | - | - | 33.05 KB | 4.89 | - | | | | | | | | | | | | | - | ImageMagick | Tiff/icc-profiles/Perceptual_RGB8_Webp.tiff | 16,155.79 μs | 855.412 μs | 46.888 μs | 1.000 | 0.00 | - | - | - | 5.93 KB | 1.00 | - | ImageSharp | Tiff/icc-profiles/Perceptual_RGB8_Webp.tiff | 84.89 μs | 2.954 μs | 0.162 μs | 0.005 | 0.00 | 6.7139 | 1.3428 | - | 110.75 KB | 18.66 | - | | | | | | | | | | | | | - | ImageMagick | Tiff/icc-profiles/Perceptual_RGB8.tiff | 17,147.35 μs | 823.088 μs | 45.116 μs | 1.000 | 0.00 | - | - | - | 6.75 KB | 1.00 | - | ImageSharp | Tiff/icc-profiles/Perceptual_RGB8.tiff | 69.64 μs | 3.953 μs | 0.217 μs | 0.004 | 0.00 | 1.9531 | - | - | 32.59 KB | 4.83 | + | Method | TestImage | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | + |------------ |-------------------------------------------- |-------------:|-------------:|-----------:|------:|--------:|----------:|----------:|---------:|-----------:|------------:| + | ImageMagick | Tiff/icc-profiles/Perceptual_CIELAB.tiff | 1,235.19 us | 170.571 us | 9.350 us | 1.00 | 0.01 | - | - | - | 6.32 KB | 1.00 | + | ImageSharp | Tiff/icc-profiles/Perceptual_CIELAB.tiff | 157.48 us | 39.497 us | 2.165 us | 0.13 | 0.00 | 1.7090 | - | - | 29.55 KB | 4.67 | + | | | | | | | | | | | | | + | ImageMagick | Tiff/icc-profiles/Perceptual_CMYK.tiff | 57,363.65 us | 2,535.386 us | 138.973 us | 1.000 | 0.00 | - | - | - | 7.73 KB | 1.00 | + | ImageSharp | Tiff/icc-profiles/Perceptual_CMYK.tiff | 446.96 us | 260.416 us | 14.274 us | 0.008 | 0.00 | 1018.5547 | 1001.9531 | 995.1172 | 5969.18 KB | 772.46 | + | | | | | | | | | | | | | + | ImageMagick | Tiff/icc-profiles/Perceptual_RGB16.tiff | 17,495.68 us | 6,126.566 us | 335.818 us | 1.000 | 0.02 | - | - | - | 6.7 KB | 1.00 | + | ImageSharp | Tiff/icc-profiles/Perceptual_RGB16.tiff | 54.46 us | 3.044 us | 0.167 us | 0.003 | 0.00 | 1.8311 | 0.1221 | - | 30.75 KB | 4.59 | + | | | | | | | | | | | | | + | ImageMagick | Tiff/icc-profiles/Perceptual_RGB8_Webp.tiff | 17,061.02 us | 4,884.549 us | 267.739 us | 1.000 | 0.02 | - | - | - | 5.88 KB | 1.00 | + | ImageSharp | Tiff/icc-profiles/Perceptual_RGB8_Webp.tiff | 73.19 us | 2.252 us | 0.123 us | 0.004 | 0.00 | 6.5918 | 1.0986 | - | 108.77 KB | 18.50 | + | | | | | | | | | | | | | + | ImageMagick | Tiff/icc-profiles/Perceptual_RGB8.tiff | 17,393.21 us | 2,057.046 us | 112.754 us | 1.000 | 0.01 | - | - | - | 6.7 KB | 1.00 | + | ImageSharp | Tiff/icc-profiles/Perceptual_RGB8.tiff | 56.44 us | 0.346 us | 0.019 us | 0.003 | 0.00 | 1.8311 | 0.0610 | - | 30.52 KB | 4.56 | Allocated reports managed memory only. Both methods include conversion from the embedded ICC profile to sRGB. ImageMagick retains Q16 output; ImageSharp writes Rgba32. diff --git a/tests/ImageSharp.Benchmarks/Codecs/Tiff/EncodeTiff.cs b/tests/ImageSharp.Benchmarks/Codecs/Tiff/EncodeTiff.cs index 148249a57b..670f45d4a3 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Tiff/EncodeTiff.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Tiff/EncodeTiff.cs @@ -21,6 +21,11 @@ public class EncodeTiff private SDImage drawing; private Image core; + // Both libraries write into this stream. Each call truncates it, so after the first call it has the full capacity, + // and the results measure the encoders instead of the growth of a new stream. + // Truncation is necessary because the GDI+ TIFF encoder fails when the stream keeps bytes from a previous call. + private MemoryStream outputStream; + private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); [Params(TestImages.Tiff.Calliphora_RgbUncompressed)] @@ -46,6 +51,7 @@ public void ReadImages() this.core = Image.Load(this.stream); this.stream.Position = 0; this.drawing = SDImage.FromStream(this.stream); + this.outputStream = new MemoryStream(); } } @@ -54,6 +60,7 @@ public void Cleanup() { this.core.Dispose(); this.drawing.Dispose(); + this.outputStream.Dispose(); } [Benchmark(Baseline = true, Description = "System.Drawing Tiff")] @@ -65,8 +72,8 @@ public void SystemDrawing() Param = { [0] = new EncoderParameter(Encoder.Compression, (long)Cast(this.Compression)) } }; - using MemoryStream memoryStream = new(); - this.drawing.Save(memoryStream, codec, parameters); + this.outputStream.SetLength(0); + this.drawing.Save(this.outputStream, codec, parameters); } [Benchmark(Description = "ImageSharp Tiff")] @@ -78,8 +85,8 @@ public void TiffCore() TiffPhotometricInterpretation.Rgb; TiffEncoder encoder = new() { Compression = this.Compression, PhotometricInterpretation = photometricInterpretation }; - using MemoryStream memoryStream = new(); - this.core.SaveAsTiff(memoryStream, encoder); + this.outputStream.SetLength(0); + this.core.SaveAsTiff(this.outputStream, encoder); } private static ImageCodecInfo FindCodecForType(string mimeType) @@ -110,8 +117,8 @@ private static EncoderValue Cast(TiffCompression compression) public static bool IsOneBitCompression(TiffCompression compression) => compression is TiffCompression.Ccitt1D or TiffCompression.CcittGroup3Fax or TiffCompression.CcittGroup4Fax; - /* Results 28.10.2026 - BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9711) + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 11.0.100-preview.7.26381.103 [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 @@ -120,19 +127,19 @@ .NET SDK 11.0.100-preview.7.26381.103 Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 WarmupCount=3 - | Method | TestImage | Compression | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | - | ---------------------- | -------------------------------------- | --------------- | ------------: | -----------: | ----------: | ------: | --------: | ---------: | ---------: | ---------: | ----------: | ------------: | - | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | None | 177.35 μs | 36.869 μs | 2.021 μs | 1.00 | 0.01 | 166.5039 | 166.5039 | 166.5039 | 527.72 KB | 1.00 | - | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | None | 90.83 μs | 4.096 μs | 0.225 μs | 0.51 | 0.01 | 111.0840 | 111.0840 | 111.0840 | 493.18 KB | 0.93 | - | | | | | | | | | | | | | | - | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | Ccitt1D | 414.84 μs | 186.439 μs | 10.219 μs | 1.00 | 0.03 | 166.5039 | 166.5039 | 166.5039 | 527.48 KB | 1.00 | - | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | Ccitt1D | 2,418.60 μs | 220.023 μs | 12.060 μs | 5.83 | 0.13 | 89.8438 | 89.8438 | 89.8438 | 383.29 KB | 0.73 | - | | | | | | | | | | | | | | - | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | CcittGroup3Fax | 1,082.74 μs | 52.868 μs | 2.898 μs | 1.00 | 0.00 | 1.9531 | - | - | 59.6 KB | 1.00 | - | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | CcittGroup3Fax | 2,414.88 μs | 109.510 μs | 6.003 μs | 2.23 | 0.01 | 89.8438 | 89.8438 | 89.8438 | 383.22 KB | 6.43 | - | | | | | | | | | | | | | | - | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | Lzw | 1,311.54 μs | 297.942 μs | 16.331 μs | 1.00 | 0.02 | 166.0156 | 166.0156 | 166.0156 | 527.72 KB | 1.00 | - | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | Lzw | 2,533.94 μs | 28.574 μs | 1.566 μs | 1.93 | 0.02 | 121.0938 | 121.0938 | 121.0938 | 524.21 KB | 0.99 | + | Method | TestImage | Compression | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio | + |---------------------- |-------------------------------------- |--------------- |------------:|-----------:|----------:|------:|--------:|-------:|----------:|------------:| + | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | None | 92.81 us | 79.501 us | 4.358 us | 1.00 | 0.06 | 0.1221 | 3.28 KB | 1.00 | + | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | None | 11.55 us | 1.084 us | 0.059 us | 0.12 | 0.01 | 0.7477 | 12.23 KB | 3.73 | + | | | | | | | | | | | | + | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | Ccitt1D | 351.24 us | 100.679 us | 5.519 us | 1.00 | 0.02 | - | 3.28 KB | 1.00 | + | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | Ccitt1D | 2,358.26 us | 162.648 us | 8.915 us | 6.72 | 0.09 | - | 17.78 KB | 5.42 | + | | | | | | | | | | | | + | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | CcittGroup3Fax | 1,083.60 us | 171.201 us | 9.384 us | 1.00 | 0.01 | - | 3.28 KB | 1.00 | + | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | CcittGroup3Fax | 2,360.68 us | 63.778 us | 3.496 us | 2.18 | 0.02 | - | 17.71 KB | 5.40 | + | | | | | | | | | | | | + | 'System.Drawing Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | Lzw | 1,216.59 us | 110.588 us | 6.062 us | 1.00 | 0.01 | - | 3.28 KB | 1.00 | + | 'ImageSharp Tiff' | Tiff/Calliphora_rgb_uncompressed.tiff | Lzw | 2,521.41 us | 408.319 us | 22.381 us | 2.07 | 0.02 | - | 12.09 KB | 3.68 | Allocated reports managed memory only. */ diff --git a/tests/ImageSharp.Benchmarks/Codecs/Webp/DecodeWebp.cs b/tests/ImageSharp.Benchmarks/Codecs/Webp/DecodeWebp.cs index 6370423bc8..6f6d4356fe 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Webp/DecodeWebp.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Webp/DecodeWebp.cs @@ -21,6 +21,11 @@ public class DecodeWebp private byte[] webpLosslessBytes; + // Both libraries read from these streams. Each call rewinds its stream, so the results measure the decoders only. + private MemoryStream webpLossyStream; + + private MemoryStream webpLosslessStream; + private string TestImageLossyFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImageLossy); private string TestImageLosslessFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImageLossless); @@ -39,22 +44,36 @@ public void ReadImages() this.webpLossyBytes ??= File.ReadAllBytes(this.TestImageLossyFullPath); this.webpLosslessBytes ??= File.ReadAllBytes(this.TestImageLosslessFullPath); + this.webpLossyStream ??= new MemoryStream(this.webpLossyBytes, false); + this.webpLosslessStream ??= new MemoryStream(this.webpLosslessBytes, false); + } + + /// + /// Releases the shared input streams. + /// + [GlobalCleanup] + public void Cleanup() + { + this.webpLossyStream.Dispose(); + this.webpLossyStream = null; + this.webpLosslessStream.Dispose(); + this.webpLosslessStream = null; } [Benchmark(Description = "Magick Lossy Webp")] public uint WebpLossyMagick() { MagickReadSettings settings = new() { Format = MagickFormat.WebP }; - using MemoryStream memoryStream = new(this.webpLossyBytes); - using MagickImage image = new(memoryStream, settings); + this.webpLossyStream.Position = 0; + using MagickImage image = new(this.webpLossyStream, settings); return image.Width; } [Benchmark(Description = "ImageSharp Lossy Webp")] public int WebpLossy() { - using MemoryStream memoryStream = new(this.webpLossyBytes); - using Image image = Image.Load(memoryStream); + this.webpLossyStream.Position = 0; + using Image image = Image.Load(this.webpLossyStream); return image.Height; } @@ -63,21 +82,21 @@ public uint WebpLosslessMagick() { MagickReadSettings settings = new() { Format = MagickFormat.WebP }; - using MemoryStream memoryStream = new(this.webpLosslessBytes); - using MagickImage image = new(memoryStream, settings); + this.webpLosslessStream.Position = 0; + using MagickImage image = new(this.webpLosslessStream, settings); return image.Width; } [Benchmark(Description = "ImageSharp Lossless Webp")] public int WebpLossless() { - using MemoryStream memoryStream = new(this.webpLosslessBytes); - using Image image = Image.Load(memoryStream); + this.webpLosslessStream.Position = 0; + using Image image = Image.Load(this.webpLosslessStream); return image.Height; } - /* Results 28.10.2026 - BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9711) + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 11.0.100-preview.7.26381.103 [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 @@ -86,12 +105,12 @@ .NET SDK 11.0.100-preview.7.26381.103 Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 WarmupCount=3 - | Method | TestImageLossy | TestImageLossless | Mean | Error | StdDev | Gen0 | Gen1 | Gen2 | Allocated | - | --------------------------- | ---------------------- | ------------------------- | ----------: | ---------: | ---------: | ----------: | ----------: | ----------: | -----------: | - | 'Magick Lossy Webp' | Webp/earth_lossy.webp | Webp/earth_lossless.webp | 65.04 ms | 2.051 ms | 0.112 ms | - | - | - | 6.26 KB | - | 'ImageSharp Lossy Webp' | Webp/earth_lossy.webp | Webp/earth_lossless.webp | 69.88 ms | 2.704 ms | 0.148 ms | 2000.0000 | 2000.0000 | 2000.0000 | 1134.94 KB | - | 'Magick Lossless Webp' | Webp/earth_lossy.webp | Webp/earth_lossless.webp | 104.51 ms | 8.797 ms | 0.482 ms | - | - | - | 7.21 KB | - | 'ImageSharp Lossless Webp' | Webp/earth_lossy.webp | Webp/earth_lossless.webp | 104.13 ms | 7.850 ms | 0.430 ms | 2600.0000 | 2600.0000 | 2600.0000 | 1673.37 KB | + | Method | TestImageLossy | TestImageLossless | Mean | Error | StdDev | Gen0 | Gen1 | Gen2 | Allocated | + |--------------------------- |---------------------- |------------------------- |----------:|----------:|---------:|----------:|----------:|----------:|-----------:| + | 'Magick Lossy Webp' | Webp/earth_lossy.webp | Webp/earth_lossless.webp | 68.13 ms | 10.892 ms | 0.597 ms | - | - | - | 6.23 KB | + | 'ImageSharp Lossy Webp' | Webp/earth_lossy.webp | Webp/earth_lossless.webp | 71.58 ms | 21.414 ms | 1.174 ms | 2000.0000 | 2000.0000 | 2000.0000 | 1220.14 KB | + | 'Magick Lossless Webp' | Webp/earth_lossy.webp | Webp/earth_lossless.webp | 105.74 ms | 7.433 ms | 0.407 ms | - | - | - | 7.19 KB | + | 'ImageSharp Lossless Webp' | Webp/earth_lossy.webp | Webp/earth_lossless.webp | 105.98 ms | 4.747 ms | 0.260 ms | 2600.0000 | 2600.0000 | 2600.0000 | 1706.99 KB | Allocated reports managed memory only. */ diff --git a/tests/ImageSharp.Benchmarks/Codecs/Webp/EncodeWebp.cs b/tests/ImageSharp.Benchmarks/Codecs/Webp/EncodeWebp.cs index 4340d2f7a5..dd04a644a9 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Webp/EncodeWebp.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Webp/EncodeWebp.cs @@ -19,6 +19,10 @@ public class EncodeWebp private MagickImage webpMagick; private Image webp; + // Both libraries write into this stream. Each call rewinds it, so after the first call it has the full capacity, + // and the results measure the encoders instead of the growth of a new stream. + private MemoryStream outputStream; + private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); [Params(TestImages.Png.Bike)] // The bike image will have all 3 transforms as lossless webp. @@ -31,6 +35,7 @@ public void ReadImages() { this.webp = Image.Load(this.TestImageFullPath); this.webpMagick = new MagickImage(this.TestImageFullPath); + this.outputStream = new MemoryStream(); } } @@ -39,12 +44,13 @@ public void Cleanup() { this.webp.Dispose(); this.webpMagick.Dispose(); + this.outputStream.Dispose(); } [Benchmark(Description = "Magick Webp Lossy")] public void MagickWebpLossy() { - using MemoryStream memoryStream = new(); + this.outputStream.Position = 0; WebPWriteDefines defines = new() { @@ -57,14 +63,14 @@ public void MagickWebpLossy() }; this.webpMagick.Quality = 75; - this.webpMagick.Write(memoryStream, defines); + this.webpMagick.Write(this.outputStream, defines); } [Benchmark(Description = "ImageSharp Webp Lossy")] public void ImageSharpWebpLossy() { - using MemoryStream memoryStream = new(); - this.webp.Save(memoryStream, new WebpEncoder() + this.outputStream.Position = 0; + this.webp.Save(this.outputStream, new WebpEncoder() { FileFormat = WebpFileFormatType.Lossy, Method = WebpEncodingMethod.Level4, @@ -78,7 +84,7 @@ public void ImageSharpWebpLossy() [Benchmark(Baseline = true, Description = "Magick Webp Lossless")] public void MagickWebpLossless() { - using MemoryStream memoryStream = new(); + this.outputStream.Position = 0; WebPWriteDefines defines = new() { Lossless = true, @@ -86,14 +92,14 @@ public void MagickWebpLossless() }; this.webpMagick.Quality = 75; - this.webpMagick.Write(memoryStream, defines); + this.webpMagick.Write(this.outputStream, defines); } [Benchmark(Description = "ImageSharp Webp Lossless")] public void ImageSharpWebpLossless() { - using MemoryStream memoryStream = new(); - this.webp.Save(memoryStream, new WebpEncoder() + this.outputStream.Position = 0; + this.webp.Save(this.outputStream, new WebpEncoder() { FileFormat = WebpFileFormatType.Lossless, Method = WebpEncodingMethod.Level4, @@ -105,8 +111,8 @@ public void ImageSharpWebpLossless() }); } - /* Results 28.10.2026 - BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9711) + /* Results 12.11.2026 + BenchmarkDotNet v0.15.8, Windows 11 (10.0.26300.9818) AMD RYZEN AI MAX+ 395 w/ Radeon 8060S 3.00GHz, 1 CPU, 32 logical and 16 physical cores .NET SDK 11.0.100-preview.7.26381.103 [Host] : .NET 10.0.14 (10.0.14, 10.0.1426.52326), X64 RyuJIT x86-64-v4 @@ -115,12 +121,12 @@ .NET SDK 11.0.100-preview.7.26381.103 Arguments=/p:DebugType=portable IterationCount=3 LaunchCount=1 WarmupCount=3 - | Method | TestImage | Mean | Error | StdDev | Ratio | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | - | --------------------------- | ------------- | ----------: | ----------: | ---------: | ------: | ----------: | ----------: | ----------: | -----------: | ------------: | - | 'Magick Webp Lossy' | Png/Bike.png | 13.60 ms | 1.243 ms | 0.068 ms | 0.08 | - | - | - | 58.99 KB | 0.12 | - | 'ImageSharp Webp Lossy' | Png/Bike.png | 40.14 ms | 3.336 ms | 0.183 ms | 0.25 | - | - | - | 259.71 KB | 0.51 | - | 'Magick Webp Lossless' | Png/Bike.png | 162.98 ms | 4.846 ms | 0.266 ms | 1.00 | - | - | - | 508.96 KB | 1.00 | - | 'ImageSharp Webp Lossless' | Png/Bike.png | 102.26 ms | 46.199 ms | 2.532 ms | 0.63 | 2000.0000 | 2000.0000 | 2000.0000 | 3898.44 KB | 7.66 | + | Method | TestImage | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Gen2 | Allocated | Alloc Ratio | + |--------------------------- |------------- |----------:|----------:|---------:|------:|--------:|----------:|----------:|----------:|-----------:|------------:| + | 'Magick Webp Lossy' | Png/Bike.png | 13.87 ms | 2.173 ms | 0.119 ms | 0.08 | 0.00 | - | - | - | 2.83 KB | 0.59 | + | 'ImageSharp Webp Lossy' | Png/Bike.png | 41.50 ms | 3.320 ms | 0.182 ms | 0.25 | 0.00 | - | - | - | 285.93 KB | 59.41 | + | 'Magick Webp Lossless' | Png/Bike.png | 166.12 ms | 23.937 ms | 1.312 ms | 1.00 | 0.01 | - | - | - | 4.81 KB | 1.00 | + | 'ImageSharp Webp Lossless' | Png/Bike.png | 105.27 ms | 59.800 ms | 3.278 ms | 0.63 | 0.02 | 2200.0000 | 2200.0000 | 2200.0000 | 4815.18 KB | 1,000.56 | Allocated reports managed memory only. Ratio uses Magick Webp Lossless as the baseline for every method, including lossy encoding.