From 5e0a56ae40dd967c0a938001ee2648bf7e6cd3ee Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Tue, 6 Sep 2016 13:49:11 +0200 Subject: [PATCH] added Buffer.MemoryCopy to ArrayCopy benchmarks Former-commit-id: b9a1e1984fcfb6fc9de15c34d8974d6c2e4cae33 Former-commit-id: 67358f5a7881048b75c279a9f739fd6a3b216824 Former-commit-id: bd9072f99fdad14282bb1eda34a940ae72440e9a --- .../General/ArrayCopy.cs | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/tests/ImageProcessorCore.Benchmarks/General/ArrayCopy.cs b/tests/ImageProcessorCore.Benchmarks/General/ArrayCopy.cs index 349c94fa3..8ebe45430 100644 --- a/tests/ImageProcessorCore.Benchmarks/General/ArrayCopy.cs +++ b/tests/ImageProcessorCore.Benchmarks/General/ArrayCopy.cs @@ -7,25 +7,44 @@ namespace ImageProcessorCore.Benchmarks.General public class ArrayCopy { - private double[] source = new double[10000]; + [Params(100, 1000, 10000)] + public int Count { get; set; } - [Benchmark(Baseline = true, Description = "Copy using Array.Copy()")] - public double CopyArray() - { + byte[] source, destination; - double[] destination = new double[10000]; - Array.Copy(source, destination, 10000); + long sizeInBytes; - return destination[0]; + [Setup] + public void SetUp() + { + source = new byte[Count]; + destination = new byte[Count]; + } + + [Benchmark(Baseline = true, Description = "Copy using Array.Copy()")] + public void CopyArray() + { + Array.Copy(source, destination, Count); } [Benchmark(Description = "Copy using Unsafe")] - public unsafe double CopyUnsafe() + public unsafe void CopyUnsafe() { - double[] destination = new double[10000]; - Unsafe.Copy(Unsafe.AsPointer(ref destination), ref source); + fixed (byte* pinnedDestination = destination) + fixed (byte* pinnedSource = source) + { + Unsafe.CopyBlock(pinnedSource, pinnedDestination, (uint)Count); + } + } - return destination[0]; + [Benchmark(Description = "Copy using Buffer.MemoryCopy")] + public unsafe void CopyUsingBufferMemoryCopy() + { + fixed (byte* pinnedDestination = destination) + fixed (byte* pinnedSource = source) + { + Buffer.MemoryCopy(pinnedSource, pinnedDestination, Count, Count); + } } } }