fork(2) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5.  
  6. public static class ConsoleSandbox
  7. {
  8. public class PropA
  9. {
  10. public int a { get; set; }
  11. public int b { get; set; }
  12. }
  13. public class PropX
  14. {
  15. public int a { get; set; }
  16. public int b { get; set; }
  17. }
  18. private static void Main()
  19. {
  20. List<PropA> first = new List<PropA>();
  21. List<PropX> second = new List<PropX>();
  22.  
  23. int numItems = 5000;
  24. Random generator = new Random(0);
  25. for (int i = 0; i < numItems; i++)
  26. {
  27. first.Add(new PropA() { b = i });
  28. second.Add(new PropX() { b = i });
  29. }
  30.  
  31. first.Shuffle(generator);
  32. second.Shuffle(generator);
  33.  
  34. Stopwatch watch = new Stopwatch();
  35. watch.Start();
  36.  
  37. var query = from firstItem in first
  38. join secondItem in second
  39. on firstItem.b equals secondItem.b
  40. select firstItem;
  41. Console.WriteLine(query.Count());
  42. var firstDuration = watch.Elapsed;
  43. Console.WriteLine(watch.Elapsed);
  44. watch.Reset();
  45. watch.Start();
  46.  
  47. var query2 = first.Where(a => second.Any(x => x.b == a.b));
  48.  
  49. Console.WriteLine(query2.Count());
  50. Console.WriteLine(watch.Elapsed);
  51.  
  52. Console.WriteLine();
  53. Console.WriteLine(firstDuration - watch.Elapsed);
  54.  
  55. Console.WriteLine();
  56. Console.WriteLine("press any key to exit . . .");
  57. Console.ReadKey();
  58. }
  59.  
  60. public static void Shuffle<T>(this IList<T> list, Random generator)
  61. {
  62. for (int i = list.Count - 1; i > 0; i--)
  63. {
  64. int next = generator.Next(i + 1);
  65. T temp = list[i];
  66. list[i] = list[next];
  67. list[next] = temp;
  68. }
  69. }
  70. }
Success #stdin #stdout 2.45s 35816KB
stdin
Standard input is empty
stdout
5000
00:00:00.0535029
5000
00:00:02.3779080

-00:00:02.3248531

press any key to exit . . .