Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7.  
  8. namespace TPLPractice
  9. {
  10. public class Program
  11. {
  12. // ReSharper disable once AsyncConverter.AsyncMethodNamingHighlighting
  13. public static Task Main(string[] args) => new Program().StartAsync();
  14.  
  15. public async Task StartAsync()
  16. {
  17. Console.Clear();
  18. var nums = new List<int>();
  19. var result = new List<long>();
  20. var r = new Random();
  21. var sw = new Stopwatch();
  22. var taskQueue = new List<Task<long>>();
  23. for (int i = 0; i < 10; i++)
  24. nums.Add(r.Next(10000, 500000));
  25. Console.WriteLine(string.Join(", ", nums));
  26. Console.WriteLine("The program will attempt to calculate the prime number for each of the numbers listed above.");
  27. Console.WriteLine("The purpose of the test is to simulate heavy CPU workload and demonstrate each concurrent APIs.");
  28. Console.WriteLine("Press any key to start.");
  29. Console.WriteLine();
  30. Console.WriteLine();
  31. Console.ReadKey();
  32. Console.WriteLine("Prime number calculation (foreach)");
  33. sw.Start();
  34. foreach (int num in nums)
  35. result.Add(await FindPrimeNumberAsync(num).ConfigureAwait(false));
  36. sw.Stop();
  37. result = PrintResult(result, sw);
  38. Console.WriteLine();
  39. sw.Reset();
  40.  
  41. Console.WriteLine("Prime number calculation (Task.WhenAll)");
  42. sw.Start();
  43. taskQueue.AddRange(nums.Select(FindPrimeNumberAsync));
  44. result.AddRange(await Task.WhenAll(taskQueue).ConfigureAwait(false));
  45. Console.WriteLine();
  46. taskQueue.Clear();
  47. sw.Stop();
  48. result = PrintResult(result, sw);
  49. Console.WriteLine();
  50. sw.Reset();
  51.  
  52. Console.WriteLine("Prime number calculation (Task.WhenAll, with each task wrapped in Task.Run)");
  53. sw.Start();
  54. taskQueue.AddRange(nums.Select(num => Task.Run(() => FindPrimeNumberAsync(num))));
  55. result.AddRange(await Task.WhenAll(taskQueue).ConfigureAwait(false));
  56. taskQueue.Clear();
  57. sw.Stop();
  58. PrintResult(result, sw);
  59. Console.WriteLine();
  60. sw.Reset();
  61.  
  62. Console.WriteLine("Prime number calculation (Parallel.ForEach)");
  63. sw.Start();
  64. result.AddRange(await CrunchNumbersForEachAsync(nums).ConfigureAwait(false));
  65. sw.Stop();
  66. PrintResult(result, sw);
  67. Console.WriteLine();
  68. sw.Reset();
  69.  
  70. Console.ReadKey();
  71. }
  72.  
  73. public List<long> PrintResult(List<long> results, Stopwatch sw)
  74. {
  75. Console.WriteLine(string.Join(", ", results));
  76. Console.WriteLine($"Finished after {sw.Elapsed}.");
  77. results.Clear();
  78. return results;
  79. }
  80.  
  81. public Task<List<long>> CrunchNumbersForEachAsync(IEnumerable<int> nums)
  82. {
  83. var concurrentBag = new ConcurrentBag<long>();
  84. Parallel.ForEach(nums,
  85. async num => { concurrentBag.Add(await FindPrimeNumberAsync(num).ConfigureAwait(false)); });
  86. return Task.FromResult(concurrentBag.ToList());
  87. }
  88.  
  89. public Task<long> FindPrimeNumberAsync(int n)
  90. {
  91. int count = 0;
  92. long a = 2;
  93. while (count < n)
  94. {
  95. long b = 2;
  96. int prime = 1;
  97. while (b * b <= a)
  98. {
  99. if (a % b == 0)
  100. {
  101. prime = 0;
  102. break;
  103. }
  104. b++;
  105. }
  106. if (prime > 0)
  107. count++;
  108. a++;
  109. }
  110. return Task.FromResult(--a);
  111. }
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement