-
-
Save iamkisly/980e832ce2ec65f77c002e23eb69f535 to your computer and use it in GitHub Desktop.
ArraysCopy Benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Configs; | |
using BenchmarkDotNet.Exporters.Csv; | |
using BenchmarkDotNet.Exporters; | |
using BenchmarkDotNet.Jobs; | |
using BenchmarkDotNet.Running; | |
using BenchmarkDotNet.Diagnosers; | |
public static class Generator | |
{ | |
public static int[] GetRandomArray(int length) | |
{ | |
int[] array = new int[length]; | |
for (int i = 0; i < length; i++) | |
{ | |
array[i] = new Random(DateTime.Now.Millisecond).Next(int.MinValue, int.MaxValue); | |
} | |
System.Threading.Thread.Sleep(2000); | |
return array; | |
} | |
public static Dictionary<int, int[]> RandomArray = new Dictionary<int, int[]>(); | |
public static int[] possibleLength = new int[] {8, 16, 32, 64, 128, 256, 512, 1024, 4096, 16384, 65536}; | |
static Generator() | |
{ | |
for(int i = 0; i < possibleLength.Length; i++) | |
{ | |
RandomArray[possibleLength[i]] = GetRandomArray(possibleLength[i]); | |
} | |
} | |
} | |
[SimpleJob(RuntimeMoniker.Net60)] | |
[SimpleJob(RuntimeMoniker.Net70)] | |
[SimpleJob(RuntimeMoniker.Net80)] | |
public class UnitTest1 | |
{ | |
public class Config : ManualConfig | |
{ | |
public Config() | |
{ | |
Add(CsvMeasurementsExporter.Default); | |
Add(RPlotExporter.Default); | |
} | |
} | |
[Params(1000)] | |
public int times; | |
[Params(8, 16, 32, 64, 128, 256, 512, 1024, 4096, 16384, 65536)] | |
public int arrayLength; | |
int[] dst = new int[65536*2]; | |
[Benchmark] | |
public void TestArrayCopy() | |
{ | |
for (var j = 0; j < times; j++) | |
{ | |
Generator.RandomArray[arrayLength].CopyTo(dst, 0); | |
} | |
} | |
[Benchmark] | |
public void TestSingleSpanCopy() | |
{ | |
var dstSpan = dst.AsSpan(); | |
for (var j = 0; j < times; j++) | |
{ | |
Generator.RandomArray[arrayLength].CopyTo(dstSpan); | |
} | |
} | |
[Benchmark] | |
public void TestDoubleSpanCopy() | |
{ | |
var srcSpan = Generator.RandomArray[arrayLength].AsSpan(); | |
var dstSpan = dst.AsSpan(); | |
for (var j = 0; j < times; j++) | |
{ | |
srcSpan.CopyTo(dstSpan); | |
} | |
} | |
[Benchmark] | |
public void BufferCopy() | |
{ | |
for (var j = 0; j < times; j++) | |
{ | |
System.Buffer.BlockCopy(Generator.RandomArray[arrayLength], 0, dst, 0, sizeof(int) * arrayLength); | |
} | |
} | |
} | |
public static class Program | |
{ | |
public static void Main() | |
{ | |
foreach (var i in Generator.possibleLength) | |
{ | |
Console.WriteLine($"{i:X6} - {Generator.RandomArray[i][0]:X4} {Generator.RandomArray[i][1]:X4}"); | |
} | |
var summary = BenchmarkRunner.Run<UnitTest1>(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment