diff --git a/collections-arrays/HowToMergeArrays/HowToMergeArrays/HowToMergeArrays.csproj b/collections-arrays/HowToMergeArrays/HowToMergeArrays/HowToMergeArrays.csproj
index 79e7148fd0..62be4f867f 100644
--- a/collections-arrays/HowToMergeArrays/HowToMergeArrays/HowToMergeArrays.csproj
+++ b/collections-arrays/HowToMergeArrays/HowToMergeArrays/HowToMergeArrays.csproj
@@ -2,13 +2,13 @@
Exe
- net6.0
+ net10.0
enable
enable
-
+
diff --git a/collections-arrays/HowToMergeArrays/HowToMergeArrays/MergeArrayBenchmark.cs b/collections-arrays/HowToMergeArrays/HowToMergeArrays/MergeArrayBenchmark.cs
index 4fb2d0ba59..61bb39b2f2 100644
--- a/collections-arrays/HowToMergeArrays/HowToMergeArrays/MergeArrayBenchmark.cs
+++ b/collections-arrays/HowToMergeArrays/HowToMergeArrays/MergeArrayBenchmark.cs
@@ -6,7 +6,7 @@ namespace HowToMergeArrays
[MemoryDiagnoser, Orderer(SummaryOrderPolicy.FastestToSlowest)]
public class MergeArrayBenchmark
{
- private static readonly int _arraySize = 50000;
+ private static readonly int _arraySize = 1000;
public int ArraySize
{
@@ -29,12 +29,12 @@ public int[] MergeUsingArrayCopyWithNewArray(int[] firstArray, int[] secondArray
public int[] MergeUsingArrayCopyWithResize(int[] firstArray, int[] secondArray)
{
var originalFirstArrayLength = firstArray.Length;
- int[] firstTempArray = Array.Empty();
+ var combinedArray = firstArray;
- Array.Resize(ref firstTempArray, firstArray.Length + secondArray.Length);
- Array.Copy(secondArray, 0, firstTempArray, originalFirstArrayLength, secondArray.Length);
+ Array.Resize(ref combinedArray, firstArray.Length + secondArray.Length);
+ Array.Copy(secondArray, 0, combinedArray, originalFirstArrayLength, secondArray.Length);
- return firstTempArray;
+ return combinedArray;
}
[Benchmark]
@@ -48,6 +48,17 @@ public int[] MergeUsingArrayCopyTo(int[] firstArray, int[] secondArray)
return combinedArray;
}
+ [Benchmark]
+ [ArgumentsSource(nameof(GetSourceArrayPopulatedWithNumbers))]
+ public int[] MergeUsingSpanCopyTo(int[] firstArray, int[] secondArray)
+ {
+ var combinedArray = new int[firstArray.Length + secondArray.Length];
+ firstArray.AsSpan().CopyTo(combinedArray);
+ secondArray.AsSpan().CopyTo(combinedArray.AsSpan(firstArray.Length));
+
+ return combinedArray;
+ }
+
[Benchmark]
[ArgumentsSource(nameof(GetSourceArrayPopulatedWithNumbers))]
public int[] MergeUsingLinqConcat(int[] firstArray, int[] secondArray)
@@ -71,7 +82,16 @@ public int[] MergeUsingLinqUnion(int[] firstArray, int[] secondArray)
public int[] MergeUsingLinqSelectMany(int[] firstArray, int[] secondArray)
{
var firstAndSecondArray = new[] { firstArray, secondArray };
- var combinedArray = firstAndSecondArray.SelectMany(animal => animal).ToArray();
+ var combinedArray = firstAndSecondArray.SelectMany(array => array).ToArray();
+
+ return combinedArray;
+ }
+
+ [Benchmark]
+ [ArgumentsSource(nameof(GetSourceArrayPopulatedWithNumbers))]
+ public int[] MergeUsingCollectionExpression(int[] firstArray, int[] secondArray)
+ {
+ int[] combinedArray = [.. firstArray, .. secondArray];
return combinedArray;
}
@@ -82,8 +102,8 @@ public int[] MergeUsingBlockCopy(int[] firstArray, int[] secondArray)
{
var combinedArray = new int[firstArray.Length + secondArray.Length];
- Buffer.BlockCopy(firstArray, 0, combinedArray, 0, firstArray.Length);
- Buffer.BlockCopy(secondArray, 0, combinedArray, firstArray.Length, secondArray.Length);
+ Buffer.BlockCopy(firstArray, 0, combinedArray, 0, firstArray.Length * sizeof(int));
+ Buffer.BlockCopy(secondArray, 0, combinedArray, firstArray.Length * sizeof(int), secondArray.Length * sizeof(int));
return combinedArray;
}
@@ -112,8 +132,8 @@ public int[] MergeUsingNewArrayManually(int[] firstArray, int[] secondArray)
public IEnumerable