Created
December 22, 2017 08:00
-
-
Save alex-fomin/23c6ffbe0a7279e14dc6d025b0424b34 to your computer and use it in GitHub Desktop.
List vs LINQ
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.Collections.Generic; | |
using System.Linq; | |
using BenchmarkDotNet.Running; | |
using BenchmarkDotNet.Attributes; | |
namespace ListVsEnumerable | |
{ | |
[MemoryDiagnoser] | |
public class ListTest | |
{ | |
private const int Count = 100000; | |
private const int Divider = 20; | |
[Benchmark] | |
public List<int> FilterEnumerable() | |
{ | |
var items = Enumerable.Range(0, Count).ToList(); | |
return items.Where(x => x % Divider == 0).ToList(); | |
} | |
[Benchmark] | |
public List<int> FilterList() | |
{ | |
var items = Enumerable.Range(0, Count).ToList(); | |
for (var i = 0; i < items.Count; i++) | |
{ | |
if (items[i] % Divider == 0) | |
{ | |
items.RemoveAt(i); | |
i--; | |
} | |
} | |
return items; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run<ListTest>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment