SHARE
TWEET

Untitled

a guest Oct 19th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using BenchmarkDotNet.Attributes;
  5.  
  6. namespace misc_bench
  7. {
  8.     [MemoryDiagnoser]
  9.     public class Benchmarks
  10.     {
  11.  
  12.         private readonly int[] _items = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  13.  
  14.         [Benchmark]
  15.         public List<int> LinqSelectWhereToList()
  16.         {
  17.             return _items.Where(x => x % 2 == 0).Select(x => x * 2).ToList();
  18.         }
  19.  
  20.         [Benchmark]
  21.         public List<int> NewLinqSelectWhereToList()
  22.         {
  23.             return new MyList(_items).Where(x => x % 2 == 0).Select(x => x * 2).ToList();
  24.         }
  25.  
  26.         [Benchmark]
  27.         public List<int> SelectWhereToListForEach()
  28.         {
  29.             var list = new List<int>();
  30.             foreach (var item in _items)
  31.             {
  32.                 if (item % 2 == 0)
  33.                 {
  34.                     list.Add(item * 2);
  35.                 }
  36.             }
  37.             return list;
  38.         }
  39.     }
  40. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top