Skip to content

Instantly share code, notes, and snippets.

@alex-fomin
Created December 22, 2017 08:00
Show Gist options
  • Save alex-fomin/23c6ffbe0a7279e14dc6d025b0424b34 to your computer and use it in GitHub Desktop.
Save alex-fomin/23c6ffbe0a7279e14dc6d025b0424b34 to your computer and use it in GitHub Desktop.
List vs LINQ
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