fork(10) download
  1. using System;
  2. using System.Linq;
  3. using System.Diagnostics;
  4. using System.Collections.Generic;
  5.  
  6. public class Test
  7. {
  8. class Point
  9. {
  10. public int X { get; set; }
  11. public int Y { get; set; }
  12. }
  13.  
  14. class Program
  15. {
  16. static void Main()
  17. {
  18. var random = new Random();
  19. List<Point> points = Enumerable.Range(0, 10000000).Select(x => new Point { X = random.Next(1000), Y = random.Next(1000) }).ToList();
  20.  
  21. int conditionValue = 250;
  22. Console.WriteLine($"Condition value: {conditionValue}");
  23.  
  24. Stopwatch sw = new Stopwatch();
  25. sw.Start();
  26.  
  27. int hitCount1 = 0;
  28. var points1 = points.Where(x =>
  29. {
  30. hitCount1++;
  31. return x.X < conditionValue;
  32. }).Select(x =>
  33. {
  34. hitCount1++;
  35. return x.Y;
  36. }).ToArray();
  37.  
  38. sw.Stop();
  39. Console.WriteLine($"Where -> Select: {sw.ElapsedMilliseconds} ms, {hitCount1} hits");
  40.  
  41. sw.Restart();
  42.  
  43. int hitCount2 = 0;
  44. var points2 = points.Select(x =>
  45. {
  46. hitCount2++;
  47. return x.Y;
  48. }).Where(x =>
  49. {
  50. hitCount2++;
  51. return x < conditionValue;
  52. }).ToArray();
  53.  
  54. sw.Stop();
  55. Console.WriteLine($"Select -> Where: {sw.ElapsedMilliseconds} ms, {hitCount2} hits");
  56.  
  57. Console.ReadLine();
  58. }
  59. }
  60. }
Success #stdin #stdout 2.26s 30880KB
stdin
Standard input is empty
stdout
Condition value: 250
Where -> Select: 266 ms, 12502408 hits
Select -> Where: 387 ms, 20000000 hits