Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test perf allocation/fillup between an Array, a List and a Dictionary
// Check the performance of using an Array (preallocated) vs a List<T>, vs a Dictionary<string, T>
// both in terms of CPU and memory
// * Summary *
//
// BenchmarkDotNet=v0.9.7.0
// OS=Microsoft Windows NT 6.2.9200.0
// Processor=Intel(R) Core(TM) i7-4770 CPU 3.40GHz, ProcessorCount=8
// Frequency=3319351 ticks, Resolution=301.2637 ns, Timer=TSC
// HostCLR=MS.NET 4.0.30319.42000, Arch=32-bit RELEASE
// JitModules=clrjit-v4.6.1080.0
//
// Type=ArrayVsDictionary Mode=Throughput LaunchCount=2
// WarmupCount=2 TargetCount=10
//
// Method | Size | Median | StdDev | Gen 0 | Gen 1 | Gen 2 | Bytes Allocated/Op |
// -------------- |----- |------------ |---------- |--------- |------ |------ |------------------- |
// NewArray | 1 | 6.2508 ns | 0.0555 ns | 29.42 | - | - | 5.76 |
// NewList | 1 | 24.9570 ns | 0.2443 ns | 92.25 | - | - | 18.26 |
// NewDictionary | 1 | 57.0320 ns | 0.8048 ns | 230.75 | - | - | 45.27 |
// NewArray | 2 | 9.8243 ns | 0.2425 ns | 35.63 | - | - | 7.02 |
// NewList | 2 | 32.3238 ns | 0.4841 ns | 94.62 | - | - | 18.72 |
// NewDictionary | 2 | 77.6402 ns | 0.5668 ns | 220.02 | - | - | 43.17 |
// NewArray | 4 | 16.9812 ns | 0.2467 ns | 50.13 | - | - | 9.83 |
// NewList | 4 | 42.3312 ns | 0.2819 ns | 94.62 | - | - | 18.72 |
// NewDictionary | 4 | 202.1413 ns | 1.2359 ns | 492.48 | - | - | 96.99 |
// NewArray | 6 | 23.0556 ns | 0.2293 ns | 62.87 | - | - | 12.34 |
// NewList | 6 | 91.0321 ns | 0.5845 ns | 167.50 | - | - | 32.92 |
// NewDictionary | 6 | 250.3487 ns | 3.0876 ns | 492.48 | - | - | 96.99 |
// NewArray | 8 | 30.0996 ns | 0.5484 ns | 75.94 | - | - | 15.09 |
// NewList | 8 | 101.1583 ns | 0.7126 ns | 176.09 | - | - | 34.61 |
// NewDictionary | 8 | 400.7448 ns | 2.4861 ns | 1,145.00 | - | - | 228.00 |
// NewArray | 10 | 37.5509 ns | 0.3046 ns | 87.86 | - | - | 17.41 |
// NewList | 10 | 149.9820 ns | 0.7045 ns | 282.95 | - | - | 56.26 |
// NewDictionary | 10 | 446.3600 ns | 1.5668 ns | 1,117.74 | - | - | 222.58 |
//
// * Diagnostic Output - MemoryDiagnoser *
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnostics.Windows;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
namespace Bench
{
public class TestBenchNewArrayVsListVsDictionary
{
private class Element
{
public string Name;
}
private Element[] defaultElements;
[Params(1, 2, 4, 6, 8, 10)]
public int Size { get; set; }
[Setup]
public void SetupData()
{
defaultElements = new Element[Size];
for (int i = 0; i < defaultElements.Length; i++)
{
defaultElements[i] = new Element() {Name = "value" + i};
}
}
[Benchmark]
public void NewArray()
{
// We setup an initial capacity, this is not fair compare to other implems
// but it shows the best you could have (between this and the List<T>)
var elements = new Element[Size];
for (int i = 0; i < defaultElements.Length; i++)
{
elements[i] = defaultElements[i];
}
}
[Benchmark]
public void NewList()
{
// We don't setup an initial capacity here
var elements = new List<Element>();
for (int i = 0; i < defaultElements.Length; i++)
{
elements.Add(defaultElements[i]);
}
}
[Benchmark]
public void NewDictionary()
{
// We don't setup an initial capcity here
var elements = new Dictionary<string, Element>();
foreach (var element in defaultElements)
{
elements.Add(element.Name, element);
}
}
}
class Program
{
static void Main(string[] args)
{
var config = ManualConfig.Create(DefaultConfig.Instance);
var gcDiagnoser = new MemoryDiagnoser();
//config.Add(new Job { Mode = Mode.SingleRun, LaunchCount = 2, WarmupCount = 2, TargetCount = 10 });
config.Add(new Job { Mode = Mode.Throughput, LaunchCount = 2, WarmupCount = 2, TargetCount = 10 });
config.Add(gcDiagnoser);
BenchmarkRunner.Run<TestBenchNewArrayVsListVsDictionary>(config);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment