Skip to content

Commit 823e785

Browse files
Merge pull request #2051 from br3aker/dp/jpeg-benchmark
Improved jpeg encoding benchmarks
2 parents a5cb2ac + 1ae157d commit 823e785

File tree

4 files changed

+201
-195
lines changed

4 files changed

+201
-195
lines changed

tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpeg.cs

Lines changed: 0 additions & 161 deletions
This file was deleted.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.Drawing.Imaging;
5+
using System.IO;
6+
using BenchmarkDotNet.Attributes;
7+
using SixLabors.ImageSharp.Formats.Jpeg;
8+
using SixLabors.ImageSharp.PixelFormats;
9+
using SixLabors.ImageSharp.Tests;
10+
using SkiaSharp;
11+
using SDImage = System.Drawing.Image;
12+
13+
namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg
14+
{
15+
/// <summary>
16+
/// Benchmark for performance comparison between other codecs.
17+
/// </summary>
18+
/// <remarks>
19+
/// This benchmarks tests baseline 4:2:0 chroma sampling path.
20+
/// </remarks>
21+
public class EncodeJpegComparison
22+
{
23+
// Big enough, 4:4:4 chroma sampling
24+
private const string TestImage = TestImages.Jpeg.Baseline.Calliphora;
25+
26+
// Change/add parameters for extra benchmarks
27+
[Params(75, 90, 100)]
28+
public int Quality;
29+
30+
private MemoryStream destinationStream;
31+
32+
// ImageSharp
33+
private Image<Rgba32> imageImageSharp;
34+
private JpegEncoder encoderImageSharp;
35+
36+
// SkiaSharp
37+
private SKBitmap imageSkiaSharp;
38+
39+
[GlobalSetup(Target = nameof(BenchmarkImageSharp))]
40+
public void SetupImageSharp()
41+
{
42+
using FileStream imageBinaryStream = File.OpenRead(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, TestImage));
43+
44+
this.imageImageSharp = Image.Load<Rgba32>(imageBinaryStream);
45+
this.encoderImageSharp = new JpegEncoder { Quality = this.Quality, ColorType = JpegColorType.YCbCrRatio420 };
46+
47+
this.destinationStream = new MemoryStream();
48+
}
49+
50+
[GlobalCleanup(Target = nameof(BenchmarkImageSharp))]
51+
public void CleanupImageSharp()
52+
{
53+
this.imageImageSharp.Dispose();
54+
this.imageImageSharp = null;
55+
56+
this.destinationStream.Dispose();
57+
this.destinationStream = null;
58+
}
59+
60+
[Benchmark(Description = "ImageSharp")]
61+
public void BenchmarkImageSharp()
62+
{
63+
this.imageImageSharp.SaveAsJpeg(this.destinationStream, this.encoderImageSharp);
64+
this.destinationStream.Seek(0, SeekOrigin.Begin);
65+
}
66+
67+
[GlobalSetup(Target = nameof(BenchmarkSkiaSharp))]
68+
public void SetupSkiaSharp()
69+
{
70+
using FileStream imageBinaryStream = File.OpenRead(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, TestImage));
71+
72+
this.imageSkiaSharp = SKBitmap.Decode(imageBinaryStream);
73+
74+
this.destinationStream = new MemoryStream();
75+
}
76+
77+
[GlobalCleanup(Target = nameof(BenchmarkSkiaSharp))]
78+
public void CleanupSkiaSharp()
79+
{
80+
this.imageSkiaSharp.Dispose();
81+
this.imageSkiaSharp = null;
82+
83+
this.destinationStream.Dispose();
84+
this.destinationStream = null;
85+
}
86+
87+
[Benchmark(Description = "SkiaSharp")]
88+
public void BenchmarkSkiaSharp()
89+
{
90+
this.imageSkiaSharp.Encode(SKEncodedImageFormat.Jpeg, this.Quality).SaveTo(this.destinationStream);
91+
this.destinationStream.Seek(0, SeekOrigin.Begin);
92+
}
93+
}
94+
}
95+
96+
/*
97+
BenchmarkDotNet=v0.13.0, OS=Windows 10.0.19044
98+
Intel Core i7-6700K CPU 4.00GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
99+
.NET SDK=6.0.100-preview.3.21202.5
100+
[Host] : .NET Core 3.1.21 (CoreCLR 4.700.21.51404, CoreFX 4.700.21.51508), X64 RyuJIT
101+
DefaultJob : .NET Core 3.1.21 (CoreCLR 4.700.21.51404, CoreFX 4.700.21.51508), X64 RyuJIT
102+
103+
104+
| Method | Quality | Mean | Error | StdDev |
105+
|----------- |-------- |----------:|----------:|----------:|
106+
| ImageSharp | 75 | 6.820 ms | 0.0374 ms | 0.0312 ms |
107+
| SkiaSharp | 75 | 16.417 ms | 0.3238 ms | 0.4747 ms |
108+
| ImageSharp | 90 | 7.849 ms | 0.1565 ms | 0.3126 ms |
109+
| SkiaSharp | 90 | 16.893 ms | 0.2200 ms | 0.2058 ms |
110+
| ImageSharp | 100 | 11.016 ms | 0.2087 ms | 0.1850 ms |
111+
| SkiaSharp | 100 | 20.410 ms | 0.2583 ms | 0.2290 ms |
112+
*/
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using BenchmarkDotNet.Attributes;
7+
using SixLabors.ImageSharp.Formats.Jpeg;
8+
using SixLabors.ImageSharp.PixelFormats;
9+
using SixLabors.ImageSharp.Tests;
10+
11+
namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg
12+
{
13+
/// <summary>
14+
/// Benchmark for all available encoding features of the Jpeg file type.
15+
/// </summary>
16+
/// <remarks>
17+
/// This benchmark does NOT compare ImageSharp to any other jpeg codecs.
18+
/// </remarks>
19+
public class EncodeJpegFeatures
20+
{
21+
// Big enough, 4:4:4 chroma sampling
22+
// No metadata
23+
private const string TestImage = TestImages.Jpeg.Baseline.Calliphora;
24+
25+
public static IEnumerable<JpegColorType> ColorSpaceValues =>
26+
new[] { JpegColorType.Luminance, JpegColorType.Rgb, JpegColorType.YCbCrRatio420, JpegColorType.YCbCrRatio444 };
27+
28+
[Params(75, 90, 100)]
29+
public int Quality;
30+
31+
[ParamsSource(nameof(ColorSpaceValues), Priority = -100)]
32+
public JpegColorType TargetColorSpace;
33+
34+
private Image<Rgb24> bmpCore;
35+
private JpegEncoder encoder;
36+
37+
private MemoryStream destinationStream;
38+
39+
[GlobalSetup]
40+
public void Setup()
41+
{
42+
using FileStream imageBinaryStream = File.OpenRead(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, TestImage));
43+
this.bmpCore = Image.Load<Rgb24>(imageBinaryStream);
44+
this.encoder = new JpegEncoder { Quality = this.Quality, ColorType = this.TargetColorSpace };
45+
this.destinationStream = new MemoryStream();
46+
}
47+
48+
[GlobalCleanup]
49+
public void Cleanup()
50+
{
51+
this.bmpCore.Dispose();
52+
this.bmpCore = null;
53+
54+
this.destinationStream.Dispose();
55+
this.destinationStream = null;
56+
}
57+
58+
[Benchmark]
59+
public void Benchmark()
60+
{
61+
this.bmpCore.SaveAsJpeg(this.destinationStream, this.encoder);
62+
this.destinationStream.Seek(0, SeekOrigin.Begin);
63+
}
64+
}
65+
}
66+
67+
/*
68+
BenchmarkDotNet=v0.13.0, OS=Windows 10.0.19044
69+
Intel Core i7-6700K CPU 4.00GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
70+
.NET SDK=6.0.100-preview.3.21202.5
71+
[Host] : .NET Core 3.1.21 (CoreCLR 4.700.21.51404, CoreFX 4.700.21.51508), X64 RyuJIT
72+
DefaultJob : .NET Core 3.1.21 (CoreCLR 4.700.21.51404, CoreFX 4.700.21.51508), X64 RyuJIT
73+
74+
75+
| Method | TargetColorSpace | Quality | Mean | Error | StdDev |
76+
|---------- |----------------- |-------- |----------:|----------:|----------:|
77+
| Benchmark | Luminance | 75 | 7.055 ms | 0.1411 ms | 0.3297 ms |
78+
| Benchmark | Rgb | 75 | 12.139 ms | 0.0645 ms | 0.0538 ms |
79+
| Benchmark | YCbCrRatio420 | 75 | 6.463 ms | 0.0282 ms | 0.0235 ms |
80+
| Benchmark | YCbCrRatio444 | 75 | 8.616 ms | 0.0422 ms | 0.0374 ms |
81+
| Benchmark | Luminance | 90 | 7.011 ms | 0.0361 ms | 0.0301 ms |
82+
| Benchmark | Rgb | 90 | 13.119 ms | 0.0947 ms | 0.0886 ms |
83+
| Benchmark | YCbCrRatio420 | 90 | 6.786 ms | 0.0328 ms | 0.0274 ms |
84+
| Benchmark | YCbCrRatio444 | 90 | 8.672 ms | 0.0772 ms | 0.0722 ms |
85+
| Benchmark | Luminance | 100 | 9.554 ms | 0.1211 ms | 0.1012 ms |
86+
| Benchmark | Rgb | 100 | 19.475 ms | 0.1080 ms | 0.0958 ms |
87+
| Benchmark | YCbCrRatio420 | 100 | 10.146 ms | 0.0585 ms | 0.0519 ms |
88+
| Benchmark | YCbCrRatio444 | 100 | 15.317 ms | 0.0709 ms | 0.0592 ms |
89+
*/

tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpegMultiple.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)