93
|
1 using System;
|
|
2 using Implab.Diagnostics;
|
|
3 using Implab.Parallels;
|
|
4 using Implab;
|
103
|
5 using System.Collections.Generic;
|
|
6 using System.Collections.Concurrent;
|
93
|
7
|
|
8 namespace MonoPlay {
|
|
9 class MainClass {
|
|
10 public static void Main(string[] args) {
|
94
|
11 if (args == null)
|
|
12 throw new ArgumentNullException("args");
|
|
13
|
103
|
14 var q1 = new MTQueue<int>();
|
104
|
15 var q2 = new Queue<int>();
|
103
|
16
|
|
17 const int count = 10000000;
|
|
18
|
108
|
19
|
103
|
20 var t1 = Environment.TickCount;
|
93
|
21
|
108
|
22 Promise<int>.CreateComposite(
|
|
23 new [] {
|
|
24 AsyncPool.InvokeNewThread(() => {
|
|
25 for (var i = 0; i < count; i++)
|
|
26 q1.Enqueue(i);
|
|
27 }),
|
|
28 AsyncPool.InvokeNewThread(() => {
|
|
29 int temp = 0;
|
|
30 for(int i =0 ; i< count ; i++)
|
|
31 while(!q1.TryDequeue(out temp)){
|
|
32 }
|
|
33 })
|
|
34 }
|
|
35 ).Join();
|
93
|
36
|
103
|
37 var t2 = Environment.TickCount;
|
|
38 Console.WriteLine("MTQueue: {0} ms", t2 - t1);
|
|
39
|
|
40 t1 = Environment.TickCount;
|
93
|
41
|
103
|
42 for (var i = 0; i < count; i++)
|
|
43 q2.Enqueue(i);
|
|
44
|
|
45 t2 = Environment.TickCount;
|
|
46 Console.WriteLine("LinkedList: {0} ms", t2 - t1);
|
|
47
|
104
|
48 q2 = new Queue<int>();
|
93
|
49
|
103
|
50 t1 = Environment.TickCount;
|
|
51
|
108
|
52 Promise<int>.CreateComposite(
|
|
53 new [] {
|
|
54 AsyncPool.InvokeNewThread(() => {
|
|
55 for (var i = 0; i < count; i++)
|
|
56 lock (q2)
|
|
57 q2.Enqueue(i);
|
|
58 }),
|
|
59 AsyncPool.InvokeNewThread(() => {
|
|
60 for(int i = 0 ; i< count ;)
|
|
61 lock(q2) {
|
|
62 if(q2.Count == 0)
|
|
63 continue;
|
|
64 q2.Dequeue();
|
|
65 i++;
|
|
66 }
|
|
67
|
|
68 })
|
|
69 }
|
|
70 ).Join();
|
|
71
|
|
72
|
103
|
73
|
|
74 t2 = Environment.TickCount;
|
|
75 Console.WriteLine("LinkedList+Lock: {0} ms", t2 - t1);
|
93
|
76
|
|
77 }
|
|
78 }
|
|
79 }
|