From 6bc9a3fc143b3c2b1311f079fc724238aebcf84c Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 17 Nov 2016 01:21:56 +0100 Subject: [PATCH] Added System.Drawing benchmarking mode --- tests/JpegBenchmark/Program.cs | 48 ++++++++++++++++++++++++-------- tests/JpegBenchmark/project.json | 1 + 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/tests/JpegBenchmark/Program.cs b/tests/JpegBenchmark/Program.cs index 260a0e4e5..e28e02c7e 100644 --- a/tests/JpegBenchmark/Program.cs +++ b/tests/JpegBenchmark/Program.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; + using System.IO; using System.Linq; using System.Numerics; @@ -23,24 +24,29 @@ namespace JpegBenchmark public static void Main(string[] args) { var allData = ReadAllFiles(); + + bool methodSystemDrawing = args.Length > 0 && args[0].ToLower().Contains("system"); + int times; - if (args.Length == 0 || !int.TryParse(args[0], out times)) + if (args.Length < 2 || !int.TryParse(args[1], out times)) { times = 20; } Console.WriteLine($"Vector.IsHardwareAccelerated == {Vector.IsHardwareAccelerated}"); + Console.WriteLine($"Method: {(methodSystemDrawing?"System.Drawing":"ImageSharp")}"); Console.WriteLine($"Decoding {allData.Length} jpegs X {times} times ..."); double estimatedTotalBlockCount = 0; Stopwatch sw = Stopwatch.StartNew(); - for (int i = 0; i < times; i++) - { - estimatedTotalBlockCount = DecodeAll(allData); - } - + + Func method = methodSystemDrawing ? (Func) DecodeMonoSystemDrawing : DecodeImageSharp; + + estimatedTotalBlockCount = DecodeAll(allData, times, method); + + sw.Stop(); Console.WriteLine($"Completed in {sw.ElapsedMilliseconds} ms"); @@ -49,16 +55,36 @@ namespace JpegBenchmark Console.ReadLine(); } - private static double DecodeAll(byte[][] allData) + private static double DecodeImageSharp(byte[] data) { + var stream = new MemoryStream(data); + Image img = new Image(stream); + return img.Width * img.Height / 64.0; + } + + private static double DecodeMonoSystemDrawing(byte[] data) + { + var stream = new MemoryStream(data); + var img = new System.Drawing.Bitmap(stream); + return img.Width * img.Height / 64.0; + } + + private static double DecodeAll(byte[][] allData, int times, Func decoderFunc) + { + + double estimatedTotalBlockCount = 0; - foreach (byte[] data in allData) + + for (int i = 0; i < times; i++) { - var stream = new MemoryStream(data); - Image img = new Image(stream); + estimatedTotalBlockCount = 0; + foreach (byte[] data in allData) + { + estimatedTotalBlockCount += decoderFunc(data); + } - estimatedTotalBlockCount += img.Width*img.Height/64.0; } + return estimatedTotalBlockCount; } } diff --git a/tests/JpegBenchmark/project.json b/tests/JpegBenchmark/project.json index dca63dcd3..eb80ce75b 100644 --- a/tests/JpegBenchmark/project.json +++ b/tests/JpegBenchmark/project.json @@ -6,6 +6,7 @@ }, "dependencies": { + "CoreCompat.System.Drawing": "1.0.0-beta006", "ImageSharp": "1.0.0-*", "Microsoft.NETCore.App": { "type": "platform",