Skip to content

Instantly share code, notes, and snippets.

@hsytkm
Created February 24, 2024 01:52
Show Gist options
  • Save hsytkm/5b1a16495ec53ea11a44cf8b02c4ed59 to your computer and use it in GitHub Desktop.
Save hsytkm/5b1a16495ec53ea11a44cf8b02c4ed59 to your computer and use it in GitHub Desktop.
WPF image speed benchmark.
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.IO;
using System.Windows.Media.Imaging;
BenchmarkRunner.Run<WpfBitmapSourceBenchmark>();
public class WpfBitmapSourceBenchmark
{
[Params(@"img100x100.bmp", @"img1000x1000.bmp")]
public string? FilePath { get; set; }
private const BitmapCreateOptions CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
private const BitmapCacheOption CacheOption = BitmapCacheOption.OnLoad;
private FileStream? _stream;
[GlobalSetup]
public void GlobalSetup()
{
_stream = File.OpenRead(FilePath!);
}
[GlobalCleanup]
public void GlobalCleanup()
{
_stream?.Dispose();
}
[Benchmark]
public BitmapImage BitmapImage_FromStream()
{
BitmapImage image = new();
image.BeginInit();
image.CreateOptions = CreateOptions;
image.CacheOption = CacheOption;
image.StreamSource = _stream;
image.EndInit();
image.Freeze();
return image;
}
[Benchmark]
public BitmapFrame BitmapFrame_FromStream()
{
BitmapFrame image = BitmapFrame.Create(_stream, CreateOptions, CacheOption);
image.Freeze();
return image;
}
[Benchmark]
public BitmapSource BitmapDecoder_FromStream()
{
BitmapDecoder decorder = BitmapDecoder.Create(_stream, CreateOptions, CacheOption);
BitmapSource image = decorder.Frames[0];
image.Freeze();
return image;
}
}

BenchmarkDotNet v0.13.12, Windows 11 (10.0.22621.3155/22H2/2022Update/SunValley2) AMD Ryzen 7 PRO 4750GE with Radeon Graphics, 1 CPU, 16 logical and 8 physical cores .NET SDK 8.0.200 [Host] : .NET 8.0.2 (8.0.224.6711), X64 RyuJIT AVX2 [AttachedDebugger] DefaultJob : .NET 8.0.2 (8.0.224.6711), X64 RyuJIT AVX2

Method FilePath Mean Error StdDev
BitmapImage_FromStream img1000x1000.bmp 4,376.1 us 10.75 us 10.06 us
BitmapFrame_FromStream img1000x1000.bmp 4,383.9 us 11.66 us 9.74 us
BitmapDecoder_FromStream img1000x1000.bmp 4,404.9 us 38.44 us 35.96 us
BitmapImage_FromStream img100x100.bmp 322.5 us 1.17 us 1.04 us
BitmapFrame_FromStream img100x100.bmp 315.8 us 1.07 us 0.95 us
BitmapDecoder_FromStream img100x100.bmp 316.2 us 0.84 us 0.75 us
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment