Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Runtime.CompilerServices;
- using System.Threading.Tasks;
- using System.Diagnostics;
- namespace ConsoleApplication2
- {
- class Program
- {
- static void Main(string[] args)
- {
- var s = new Stopwatch();
- // run some things to ensure everything is jitted
- {
- Console.WriteLine("Priming... ");
- s.Start();
- s.Stop();
- s.Restart();
- s.Stop();
- Test1();
- Test2();
- Console.WriteLine(s.Elapsed);
- Console.WriteLine("Priming complete.");
- }
- // test 1 -- use a local variable
- {
- Console.WriteLine("Local variable test... ");
- s.Restart();
- Test1();
- s.Stop();
- Console.WriteLine(s.Elapsed);
- }
- // test 2 -- use boxing
- {
- Console.WriteLine("Boxing test... ");
- s.Restart();
- Test2();
- s.Stop();
- Console.WriteLine(s.Elapsed);
- }
- }
- [MethodImpl(MethodImplOptions.NoInlining)]
- private static void Test1()
- {
- var tasks = new Task[1000000];
- for (var i = 0; i < 1000000; ++i)
- {
- var j = i;
- tasks[i] = new Task(() => ProcessItem(j));
- tasks[i].Start();
- }
- Task.WaitAll(tasks);
- }
- [MethodImpl(MethodImplOptions.NoInlining)]
- private static void Test2()
- {
- var tasks = new Task[1000000];
- for (var i = 0; i < 1000000; ++i)
- {
- tasks[i] = new Task(o => ProcessItem((int)o), i);
- tasks[i].Start();
- }
- Task.WaitAll(tasks);
- }
- [MethodImpl(MethodImplOptions.NoInlining)]
- private static void ProcessItem(int j)
- {
- ++j;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement