Created
May 3, 2022 14:46
-
-
Save piotrbrzuska/e6f59551e0866be6f7b2c19f239bfe59 to your computer and use it in GitHub Desktop.
Benchmark for https://medium.com/dotnetsafer/the-difference-between-array-and-list-in-c-memory-usage-performance-2fff9968e2b4
This file contains hidden or 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 System; | |
| using System.Collections.Generic; | |
| using BenchmarkDotNet.Attributes; | |
| using BenchmarkDotNet.Running; | |
| namespace ArrayVsListBench | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| BenchmarkRunner.Run(typeof(Program).Assembly); | |
| /* | |
| Results: | |
| | Method | iterations | Mean | Error | StdDev | Gen 0 | Gen 1 | Allocated | | |
| |------------ |----------- |----------:|----------:|----------:|--------:|--------:|----------:| | |
| | ListInt | 10000 | 14.428 us | 0.0813 us | 0.0760 us | 6.3629 | 0.7935 | 39 KB | | |
| | ListObject | 10000 | 66.661 us | 0.2376 us | 0.1984 us | 50.7813 | 16.8457 | 313 KB | | |
| | ListLong | 10000 | 15.521 us | 0.0922 us | 0.0817 us | 12.6343 | 2.5024 | 78 KB | | |
| | ListUlong | 10000 | 15.550 us | 0.1325 us | 0.1174 us | 12.6343 | 2.5024 | 78 KB | | |
| | ArrayInt | 10000 | 5.958 us | 0.0309 us | 0.0274 us | 6.3248 | - | 39 KB | | |
| | ArrayObject | 10000 | 60.852 us | 0.5399 us | 0.4786 us | 50.8423 | 16.9067 | 313 KB | | |
| | ArrayLong | 10000 | 5.560 us | 0.0928 us | 0.0775 us | 12.6572 | - | 78 KB | | |
| | ArrayUlong | 10000 | 6.930 us | 0.0927 us | 0.0822 us | 12.6572 | - | 78 KB | | |
| */ | |
| } | |
| } | |
| [MemoryDiagnoser] | |
| public class ArrayVsList | |
| { | |
| [Benchmark] | |
| [Arguments(10000)] | |
| public void ListInt(int iterations) | |
| { | |
| List<int> li = new List<int>(iterations); | |
| for (int i = 0; i < iterations; i++) | |
| { | |
| li.Add(i); | |
| } | |
| } | |
| [Benchmark] | |
| [Arguments(10000)] | |
| public void ListObject(int iterations) | |
| { | |
| List<object> li = new List<object>(iterations); | |
| for (int i = 0; i < iterations; i++) | |
| { | |
| li.Add(i); | |
| } | |
| } | |
| [Benchmark] | |
| [Arguments(10000)] | |
| public void ListLong(long iterations) | |
| { | |
| List<long> li = new List<long>((int)iterations); | |
| for (long i = 0; i < iterations; i++) | |
| { | |
| li.Add(i); | |
| } | |
| } | |
| [Benchmark] | |
| [Arguments(10000)] | |
| public void ListUlong(ulong iterations) | |
| { | |
| List<ulong> li = new List<ulong>((int)iterations); | |
| for (ulong i = 0; i < iterations; i++) | |
| { | |
| li.Add(i); | |
| } | |
| } | |
| [Benchmark] | |
| [Arguments(10000)] | |
| public void ArrayInt(int iterations) | |
| { | |
| int[] a = new int[iterations]; | |
| for (int i = 0; i < iterations; i++) | |
| { | |
| a[i] = i; | |
| } | |
| } | |
| [Benchmark] | |
| [Arguments(10000)] | |
| public void ArrayObject(int iterations) | |
| { | |
| object[] a = new object[iterations]; | |
| for (int i = 0; i < iterations; i++) | |
| { | |
| a[i] = i; | |
| } | |
| } | |
| [Benchmark] | |
| [Arguments(10000)] | |
| public void ArrayLong(long iterations) | |
| { | |
| long[] a = new long[iterations]; | |
| for (long i = 0; i < iterations; i++) | |
| { | |
| a[i] = i; | |
| } | |
| } | |
| [Benchmark] | |
| [Arguments(10000)] | |
| public void ArrayUlong(ulong iterations) | |
| { | |
| ulong[] a = new ulong[iterations]; | |
| for (ulong i = 0; i < iterations; i++) | |
| { | |
| a[i] = i; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment