Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using BenchmarkDotNet.Attributes;
- namespace misc_bench
- {
- [MemoryDiagnoser]
- public class Benchmarks
- {
- private readonly int[] _items = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
- [Benchmark]
- public List<int> LinqSelectWhereToList()
- {
- return _items.Where(x => x % 2 == 0).Select(x => x * 2).ToList();
- }
- [Benchmark]
- public List<int> NewLinqSelectWhereToList()
- {
- return new MyList(_items).Where(x => x % 2 == 0).Select(x => x * 2).ToList();
- }
- [Benchmark]
- public List<int> SelectWhereToListForEach()
- {
- var list = new List<int>();
- foreach (var item in _items)
- {
- if (item % 2 == 0)
- {
- list.Add(item * 2);
- }
- }
- return list;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement