Mercurial > pub > ImplabNet
annotate MonoPlay/Program.cs @ 119:2573b562e328 v2
Promises rewritten, added improved version of AsyncQueue
author | cin |
---|---|
date | Sun, 11 Jan 2015 19:13:02 +0300 |
parents | f3bdb7ba59b9 |
children | f1b897999260 |
rev | line source |
---|---|
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 | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
14 var q1 = new AsyncQueue<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 |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
22 new [] { |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
23 AsyncPool.InvokeNewThread(() => { |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
24 for (var i = 0; i < count; i++) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
25 q1.Enqueue(i); |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
26 }), |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
27 AsyncPool.InvokeNewThread(() => { |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
28 for (var i = 0; i < count; i++) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
29 q1.Enqueue(i); |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
30 }), |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
31 AsyncPool.InvokeNewThread(() => { |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
32 int temp = 0; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
33 int i = 0; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
34 while (i < count) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
35 if (q1.TryDequeue(out temp)) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
36 i++; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
37 }), |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
38 AsyncPool.InvokeNewThread(() => { |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
39 int temp = 0; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
40 int i = 0; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
41 while (i < count) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
42 if (q1.TryDequeue(out temp)) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
43 i++; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
44 }) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
45 } |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
46 .Combine() |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
47 .Join(); |
93 | 48 |
103 | 49 var t2 = Environment.TickCount; |
50 Console.WriteLine("MTQueue: {0} ms", t2 - t1); | |
51 | |
52 t1 = Environment.TickCount; | |
93 | 53 |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
54 for (var i = 0; i < count * 2; i++) |
103 | 55 q2.Enqueue(i); |
56 | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
57 for (var i = 0; i < count * 2; i++) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
58 q2.Dequeue(); |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
59 |
103 | 60 t2 = Environment.TickCount; |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
61 Console.WriteLine("Queue: {0} ms", t2 - t1); |
103 | 62 |
104 | 63 q2 = new Queue<int>(); |
93 | 64 |
103 | 65 t1 = Environment.TickCount; |
66 | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
67 |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
68 new [] { |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
69 AsyncPool.InvokeNewThread(() => { |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
70 for (var i = 0; i < count; i++) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
71 lock (q2) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
72 q2.Enqueue(i); |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
73 }), |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
74 AsyncPool.InvokeNewThread(() => { |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
75 for (var i = 0; i < count; i++) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
76 lock (q2) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
77 q2.Enqueue(i); |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
78 }), |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
79 AsyncPool.InvokeNewThread(() => { |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
80 for (int i = 0; i < count ;) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
81 lock (q2) { |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
82 if (q2.Count == 0) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
83 continue; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
84 q2.Dequeue(); |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
85 i++; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
86 } |
108 | 87 |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
88 }), |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
89 AsyncPool.InvokeNewThread(() => { |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
90 for (int i = 0; i < count ;) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
91 lock (q2) { |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
92 if (q2.Count == 0) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
93 continue; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
94 q2.Dequeue(); |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
95 i++; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
96 } |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
97 |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
98 }) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
99 } |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
100 .Combine() |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
101 .Join(); |
108 | 102 |
103 | |
103 | 104 |
105 t2 = Environment.TickCount; | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
106 Console.WriteLine("Queue+Lock: {0} ms", t2 - t1); |
93 | 107 |
108 } | |
109 } | |
110 } |