C#のWinFormsでSystem.DrawingとMagick.NETとOpenCVSharpでPNGファイルのデコード速度を比較

C# コンピュータ
C#

手作りのソフトなので計測なので精度は今一つですが、System.Drawingが一番速い結果になりました。
System.Drawing:22ms
Magick.NET:39ms
OpenCVSharp:33ms
Windows環境で実行しているので、納得の結果といった感じです。

ソースコード

using System.Diagnostics;
using System.Linq.Expressions;
using ImageMagick;
using OpenCvSharp;
using OpenCvSharp.Extensions;

namespace ImageReadMeasurement;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        const string imageFile = @"C:\Users\karet\Pictures\IMG_20240302_181544629_MFNR.jpg";

        using FileStream fs = new(imageFile, FileMode.Open, FileAccess.Read);
        MemoryStream ms = new();
        fs.CopyTo(ms);
        

        Button sysDrawButton = new()
        {
            Location = new System.Drawing.Point(10, 10),
            Size = new System.Drawing.Size(200, 60),
            Text = "System.Drawing"
        };
        Button magickButton = new()
        {
            Location = new System.Drawing.Point(220, 10),
            Size = new System.Drawing.Size(200, 60),
            Text = "Magick.Net"
        };
        Button cvButton = new()
        {
            Location = new System.Drawing.Point(420, 10),
            Size = new System.Drawing.Size(200, 60),
            Text = "OpenCVSharp"
        };

        PictureBox picbox = new()
        {
            Location = new System.Drawing.Point(10, 100),
            Size = new System.Drawing.Size(600, 600),
        };
        Label resultLabel = new()
        {
            Location = new System.Drawing.Point(10, 800),
            Size = new System.Drawing.Size(300, 30),
            Text = "結果:"
        };
        Controls.AddRange(
        [
            resultLabel,
            cvButton,
            magickButton,
            sysDrawButton,
            picbox,
        ]);
        Size = new System.Drawing.Size(1280, 1280);
        FormClosed += (sender, e) =>
        {
            ms?.Dispose();
        };
        
        sysDrawButton.Click += (sender, e) =>
        {
            ms.Seek(0, SeekOrigin.Begin);

            Stopwatch sw = new();
            sw.Start();
            Image img = Image.FromStream(ms);
            sw.Stop();
            picbox.Image?.Dispose();
            picbox.Image = img;
            resultLabel.Text = $"System.Drawing:{sw.ElapsedMilliseconds}ms";
        };
        magickButton.Click += (sender, e) =>
        {
            ms.Seek(0, SeekOrigin.Begin);

            Stopwatch sw = new();
            sw.Start();
            var magick = new ImageMagick.MagickImage(ms);
            Image img = magick.ToBitmap();
            sw.Stop();
            picbox.Image?.Dispose();
            picbox.Image = img;
            resultLabel.Text = $"Magick.Net:{sw.ElapsedMilliseconds}ms";
        };
        cvButton.Click += (sender, e) =>
        {
            ms.Seek(0, SeekOrigin.Begin);

            Stopwatch sw = new();
            sw.Start();
            var bytes = ms.ToArray();
            Mat mat = Cv2.ImDecode(bytes, ImreadModes.Unchanged);
            Image img = BitmapConverter.ToBitmap(mat);
            sw.Stop();
            picbox.Image?.Dispose();
            picbox.Image = img;
            resultLabel.Text = $"OpenCVSharp:{sw.ElapsedMilliseconds}ms";
        };
    }
}

実行

コメント