Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. IList<RepeaterItem> items;
  2.  
  3. var result = items
  4. .Select(item => item.FindControl("somecontrol"))
  5. .Where(ctrl => SomeCheck(ctrl))
  6. .AsParallel();
  7.  
  8. var result = items
  9. .AsParallel()
  10. .Select(item => item.FindControl("somecontrol"))
  11. .Where(ctrl => SomeCheck(ctrl));
  12.  
  13. using System;
  14. using System.Diagnostics;
  15. using System.Linq;
  16. using System.Threading;
  17.  
  18. class Test
  19. {
  20. static void Main()
  21. {
  22. var query = Enumerable.Range(0, 1000)
  23. .Select(SlowProjection)
  24. .Where(x => x > 10)
  25. .AsParallel();
  26. Stopwatch sw = Stopwatch.StartNew();
  27. int count = query.Count();
  28. sw.Stop();
  29. Console.WriteLine("Count: {0} in {1}ms", count,
  30. sw.ElapsedMilliseconds);
  31.  
  32. query = Enumerable.Range(0, 1000)
  33. .AsParallel()
  34. .Select(SlowProjection)
  35. .Where(x => x > 10);
  36. sw = Stopwatch.StartNew();
  37. count = query.Count();
  38. sw.Stop();
  39. Console.WriteLine("Count: {0} in {1}ms", count,
  40. sw.ElapsedMilliseconds);
  41. }
  42.  
  43. static int SlowProjection(int input)
  44. {
  45. Thread.Sleep(100);
  46. return input;
  47. }
  48. }
  49.  
  50. Count: 989 in 100183ms
  51. Count: 989 in 13626ms
  52.  
  53. var SlowProjection = new Func<int, int>((input) => { Thread.Sleep(100); return input; });
  54.  
  55. var Measure = new Action<string, Func<List<int>>>((title, measure) =>
  56. {
  57. Stopwatch sw = Stopwatch.StartNew();
  58. var result = measure();
  59. sw.Stop();
  60. Console.Write("{0} Time: {1}, Result: ", title, sw.ElapsedMilliseconds);
  61. foreach (var entry in result) Console.Write(entry + " ");
  62. });
  63.  
  64. Measure("Sequential", Enumerable.Range(0, 30)
  65. .Select(SlowProjection).Where(x => x > 10).ToList());
  66. Measure("Parallel", Enumerable.Range(0, 30).AsParallel()
  67. .Select(SlowProjection).Where(x => x > 10).ToList());
  68. Measure("Ordered", Enumerable.Range(0, 30).AsParallel().AsOrdered()
  69. .Select(SlowProjection).Where(x => x > 10).ToList());
  70.  
  71. Sequential Time: 6699, Result: 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
  72. Parallel Time: 1462, Result: 12 16 22 25 29 14 17 21 24 11 15 18 23 26 13 19 20 27 28
  73. Ordered Time: 1357, Result: 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement