Skip to content

Instantly share code, notes, and snippets.

@tkapin
Created December 21, 2021 17:42
Show Gist options
  • Save tkapin/b8759b1b441e4a965fa7e26e2aa10d67 to your computer and use it in GitHub Desktop.
Save tkapin/b8759b1b441e4a965fa7e26e2aa10d67 to your computer and use it in GitHub Desktop.
Concat benchmarks
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace Benchmarks;
public class Program
{
public static int Main(string[] args)
{
var summary = BenchmarkRunner.Run<ConcatImplementations>();
return 0;
}
}
public class ConcatImplementations {
[Benchmark]
public void IEnumerableConcat()
{
IEnumerable<string> args = new[] { "x", "y", "z" };
args = new[] { "a", "b" }.Concat(args);
// materialize
_ = args.ToArray();
}
[Benchmark]
public void ListInsertRange()
{
List<string> args = new() { "x", "y", "z" };
args.InsertRange(0, new[] { "a", "b" });
// convert
_ = args.ToArray();
}
}
@tkapin
Copy link
Author

tkapin commented Dec 21, 2021

Method Mean Error StdDev
IEnumerableConcat 157.4 ns 3.14 ns 4.79 ns
ListInsertRange 110.6 ns 2.20 ns 3.01 ns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment