Advertisement
Guest User

Untitled

a guest
May 23rd, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. void Main()
  2. {
  3.  
  4. Console.WriteLine("\r\n==== Distinct() without ToArray()====");
  5. var stopwatch = Stopwatch.StartNew();
  6. var results = Results().Distinct();
  7. ProcessResults(results,stopwatch);
  8.  
  9. Console.WriteLine("\r\n==== Distinct() with ToArray()====");
  10. stopwatch = Stopwatch.StartNew();
  11. results = Results().ToArray().Distinct();
  12. ProcessResults(results, stopwatch);
  13. }
  14.  
  15.  
  16. public void ProcessResults(IEnumerable<string> results, Stopwatch stopWatch) {
  17. var first = true;
  18. foreach( var item in results) {
  19. if( first) {
  20. first = false;
  21. Console.WriteLine("Time to first item: {0} ms", stopWatch.ElapsedMilliseconds );
  22. }
  23.  
  24. Console.WriteLine("{0}ms :: '{1}' ", stopWatch.ElapsedMilliseconds , item);
  25. }
  26. Console.WriteLine("Total Time to enumerate whole set: {0} ms", stopWatch.ElapsedMilliseconds );
  27.  
  28. }
  29.  
  30. public IEnumerable<string> Results() {
  31. yield return "One";
  32. System.Threading.Thread.Sleep(100); // each item takes a bit of time.
  33. yield return "Two";
  34. System.Threading.Thread.Sleep(100); // each item takes a bit of time.
  35. yield return "Three";
  36. System.Threading.Thread.Sleep(100); // each item takes a bit of time.
  37. yield return "Four";
  38. System.Threading.Thread.Sleep(100); // each item takes a bit of time.
  39. yield return "Five";
  40. System.Threading.Thread.Sleep(100); // each item takes a bit of time.
  41. yield return "Six";
  42. System.Threading.Thread.Sleep(100); // each item takes a bit of time.
  43. yield return "Seven";
  44. System.Threading.Thread.Sleep(100); // each item takes a bit of time.
  45. yield return "One";
  46. }
  47.  
  48. /** RESULTS:
  49.  
  50. ==== Distinct() without ToArray()====
  51. Time to first item: 1 ms
  52. 1ms :: 'One'
  53. 102ms :: 'Two'
  54. 203ms :: 'Three'
  55. 303ms :: 'Four'
  56. 403ms :: 'Five'
  57. 504ms :: 'Six'
  58. 605ms :: 'Seven'
  59. Total Time to enumerate whole set: 706 ms
  60.  
  61. ==== Distinct() with ToArray()====
  62. Time to first item: 703 ms
  63. 703ms :: 'One'
  64. 703ms :: 'Two'
  65. 703ms :: 'Three'
  66. 703ms :: 'Four'
  67. 703ms :: 'Five'
  68. 703ms :: 'Six'
  69. 703ms :: 'Seven'
  70. Total Time to enumerate whole set: 703 ms
  71.  
  72.  
  73. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement