Created
July 21, 2017 08:24
-
-
Save alex-fomin/8a53c35bd6e2f95c97faefbd4d192c67 to your computer and use it in GitHub Desktop.
ToArray vs ToList vs ToImmutableArray vs ToImmutableList
This file contains 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 System.Collections.Immutable; | |
using System.Linq; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
BenchmarkRunner.Run<Bench>(); | |
} | |
} | |
public class Bench | |
{ | |
private const int _count = 10000; | |
IEnumerable<int> Do(Func<IEnumerable<int>, IEnumerable<int>>f) => | |
f(f(Enumerable.Range(0, _count)).Where(x => x % 2 == 0)); | |
[Benchmark] | |
public IEnumerable<int> ToImmutableArray() => Do(x => x.ToImmutableArray()); | |
[Benchmark] | |
public IEnumerable<int> ToArray() => Do(x => x.ToArray()); | |
[Benchmark] | |
public IEnumerable<int> ToList() => Do(x => x.ToList()); | |
[Benchmark] | |
public IEnumerable<int> ToImmutableList() => Do(x => x.ToImmutableList()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Slightly modified:
Result: