Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@controlflow
Created October 26, 2019 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save controlflow/2da1d912e99265c8d786e1d4640e0a29 to your computer and use it in GitHub Desktop.
Save controlflow/2da1d912e99265c8d786e1d4640e0a29 to your computer and use it in GitHub Desktop.
BenchmarkDotNet=v0.12.0, OS=Windows 10.0.17763.805 (1809/October2018Update/Redstone5), VM=VMware
Intel Core i7-7700K CPU 4.20GHz (Kaby Lake), 1 CPU, 4 logical and 4 physical cores
.NET Core SDK=2.1.507
[Host] : .NET Core 2.1.11 (CoreCLR 4.6.27617.04, CoreFX 4.6.27617.02), X64 RyuJIT
DefaultJob : .NET Core 2.1.11 (CoreCLR 4.6.27617.04, CoreFX 4.6.27617.02), X64 RyuJIT
| Method | Count | Mean | Error | StdDev | Rank |
|--------------- |------ |------------:|----------:|----------:|-----:|
| IntArray | 1000 | 407.0 ns | 5.17 ns | 4.84 ns | 1 |
| StringArray | 1000 | 2,182.7 ns | 25.59 ns | 22.69 ns | 4 |
| StringBoxArray | 1000 | 1,673.0 ns | 9.62 ns | 8.53 ns | 2 |
| StringSpan | 1000 | 1,940.8 ns | 21.95 ns | 20.53 ns | 3 |
| IntArray | 10000 | 4,021.3 ns | 42.70 ns | 39.94 ns | 5 |
| StringArray | 10000 | 22,162.3 ns | 883.03 ns | 867.25 ns | 8 |
| StringBoxArray | 10000 | 16,833.7 ns | 203.78 ns | 190.62 ns | 6 |
| StringSpan | 10000 | 19,307.2 ns | 225.80 ns | 211.22 ns | 7 |
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
public struct StringBox {
public string Value;
}
[RankColumn]
public class ArraysBenchmark {
private int[] intArray;
private string[] stringArray;
private StringBox[] stringBoxArray;
//private Memory<string> stringMemory;
[Params(1000, 10000)]
public int Count;
[GlobalSetup]
public void Setup() {
intArray = new int[Count];
stringArray = new string[Count];
stringBoxArray = new StringBox[Count];
//stringMemory = new Memory<string>();
}
[Benchmark]
public void IntArray() {
var array = intArray;
for (var index = 0; index < array.Length; index++) {
array[index] = 42;
}
}
[Benchmark]
public void StringArray() {
var array = stringArray;
for (var index = 0; index < array.Length; index++) {
array[index] = "42";
}
}
[Benchmark]
public void StringBoxArray() {
var array = stringBoxArray;
for (var index = 0; index < array.Length; index++) {
array[index].Value = "42";
}
}
[Benchmark]
public void StringSpan() {
Span<string> span = stringArray;
for (var index = 0; index < span.Length; index++) {
span[index] = "42";
}
}
}
public class Program {
public static void Main(string[] args) {
BenchmarkRunner.Run<ArraysBenchmark>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment