Last active
August 26, 2018 09:56
-
-
Save batzen/e1be83af9f4baa95b4ddbeca4acb4c1a to your computer and use it in GitHub Desktop.
[Benchmark] ToArrayVsToList
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
namespace ToArrayVsToList | |
{ | |
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
[MemoryDiagnoser] | |
public class ToArrayVsToList | |
{ | |
private IEnumerable<string> data; | |
[Params(200, 10_000)] | |
public int Length { get; set; } | |
[GlobalSetup] | |
public void Setup() | |
{ | |
this.data = Enumerable.Repeat(string.Empty, this.Length); | |
} | |
[Benchmark] | |
public string[] ToArray() => data.ToArray(); | |
[Benchmark] | |
public List<string> ToList() => data.ToList(); | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run<ToArrayVsToList>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment