fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApplication5
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. var stopWatch = new Stopwatch();
  15. stopWatch.Start();
  16.  
  17. var list = new List<int>();
  18.  
  19. foreach (var i in Enumerable.Range(0, 10000))
  20. {
  21. list.Add(i);
  22. }
  23.  
  24. stopWatch.Stop();
  25. Console.WriteLine($"Count:{list.Count}");
  26. Console.WriteLine($"time:{stopWatch.ElapsedTicks}");
  27.  
  28. list = new List<int>();
  29. stopWatch = new Stopwatch();
  30. stopWatch.Start();
  31. Parallel.ForEach(Enumerable.Range(0, 10000), new ParallelOptions() { MaxDegreeOfParallelism = 1000 }, i =>
  32. {
  33. lock(list)
  34. {
  35. list.Add(i);
  36. }
  37. });
  38.  
  39. stopWatch.Stop();
  40. Console.WriteLine($"Count:{list.Count}");
  41. Console.WriteLine($"time:{stopWatch.ElapsedTicks}");
  42. }
  43. }
  44. }
Success #stdin #stdout 0.08s 27408KB
stdin
Standard input is empty
stdout
Count:10000
time:34208
Count:10000
time:542652