-
-
Save karenpayneoregon/dcb8d2de829e254a6f00f973382bba51 to your computer and use it in GitHub Desktop.
This file contains 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.Running; | |
using System.Numerics; | |
BenchmarkRunner.Run(typeof(ConcatBenchmark).Assembly); | |
[MemoryDiagnoser] | |
public class ConcatBenchmark | |
{ | |
private readonly int[] arr1 = new[] { 1, 2, 3, 4, 5, 0 }, | |
arr2 = new[] { 6, 7, 8, 9, 0 }; | |
[Benchmark] | |
public int[] Linq() => arr1.Merge1(arr2); | |
[Benchmark] | |
public int[] CollectionExpression() => arr1.Merge2(arr2); | |
} | |
public static class ArrayExtensions | |
{ | |
public static T[] Merge1<T>(this T[] front, T[] back) where T : INumber<T> | |
=> front.Concat(back).ToArray(); | |
public static T[] Merge2<T>(this T[] front, T[] back) where T : INumber<T> | |
=> [.. front, .. back]; | |
} |
This file contains 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
// * Summary * | |
BenchmarkDotNet v0.13.12, Windows 11 (10.0.23615.1000) | |
AMD Ryzen 9 7900X, 1 CPU, 24 logical and 12 physical cores | |
.NET SDK 8.0.100 | |
[Host] : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI | |
DefaultJob : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI | |
| Method | Mean | Error | StdDev | Gen0 | Allocated | | |
|--------------------- |----------:|----------:|----------:|-------:|----------:| | |
| Linq | 40.055 ns | 0.2545 ns | 0.2380 ns | 0.0110 | 184 B | | |
| CollectionExpression | 6.182 ns | 0.0426 ns | 0.0355 ns | 0.0043 | 72 B | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment