Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement