Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Threading.Tasks;
  4. using System.Diagnostics;
  5.  
  6. namespace ConsoleApplication2
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. var s = new Stopwatch();
  13. // run some things to ensure everything is jitted
  14. {
  15. Console.WriteLine("Priming... ");
  16. s.Start();
  17. s.Stop();
  18. s.Restart();
  19. s.Stop();
  20. Test1();
  21. Test2();
  22. Console.WriteLine(s.Elapsed);
  23. Console.WriteLine("Priming complete.");
  24. }
  25. // test 1 -- use a local variable
  26. {
  27. Console.WriteLine("Local variable test... ");
  28. s.Restart();
  29. Test1();
  30. s.Stop();
  31. Console.WriteLine(s.Elapsed);
  32. }
  33. // test 2 -- use boxing
  34. {
  35. Console.WriteLine("Boxing test... ");
  36. s.Restart();
  37. Test2();
  38. s.Stop();
  39. Console.WriteLine(s.Elapsed);
  40. }
  41. }
  42.  
  43. [MethodImpl(MethodImplOptions.NoInlining)]
  44. private static void Test1()
  45. {
  46. var tasks = new Task[1000000];
  47. for (var i = 0; i < 1000000; ++i)
  48. {
  49. var j = i;
  50. tasks[i] = new Task(() => ProcessItem(j));
  51. tasks[i].Start();
  52. }
  53. Task.WaitAll(tasks);
  54. }
  55.  
  56. [MethodImpl(MethodImplOptions.NoInlining)]
  57. private static void Test2()
  58. {
  59. var tasks = new Task[1000000];
  60. for (var i = 0; i < 1000000; ++i)
  61. {
  62. tasks[i] = new Task(o => ProcessItem((int)o), i);
  63. tasks[i].Start();
  64. }
  65. Task.WaitAll(tasks);
  66. }
  67.  
  68. [MethodImpl(MethodImplOptions.NoInlining)]
  69. private static void ProcessItem(int j)
  70. {
  71. ++j;
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement