Mercurial > pub > ImplabNet
annotate MonoPlay/Program.cs @ 123:f4d6ea6969cc v2
async queue improvements
author | cin |
---|---|
date | Tue, 13 Jan 2015 01:42:38 +0300 |
parents | 0c8685c8b56b |
children | a336cb13c6a9 |
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 | |
121 | 19 int res1 = 0, res2 = 0; |
103 | 20 var t1 = Environment.TickCount; |
93 | 21 |
121 | 22 AsyncPool.RunThread( |
120 | 23 () => { |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
24 for (var i = 0; i < count; i++) |
121 | 25 q1.Enqueue(1); |
26 Console.WriteLine("done writer #1: {0} ms", Environment.TickCount - t1); | |
120 | 27 }, |
28 () => { | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
29 for (var i = 0; i < count; i++) |
121 | 30 q1.Enqueue(2); |
31 Console.WriteLine("done writer #2: {0} ms", Environment.TickCount - t1); | |
120 | 32 }, |
33 () => { | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
34 int temp = 0; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
35 int i = 0; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
36 while (i < count) |
121 | 37 if (q1.TryDequeue(out temp)) { |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
38 i++; |
121 | 39 res1 += temp; |
40 } | |
41 Console.WriteLine("done reader #1: {0} ms", Environment.TickCount - t1); | |
120 | 42 }, |
43 () => { | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
44 int temp = 0; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
45 int i = 0; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
46 while (i < count) |
121 | 47 if (q1.TryDequeue(out temp)) { |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
48 i++; |
121 | 49 res2 += temp; |
50 } | |
51 Console.WriteLine("done reader #2: {0} ms", Environment.TickCount - t1); | |
120 | 52 } |
53 ) | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
54 .Combine() |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
55 .Join(); |
93 | 56 |
122
0c8685c8b56b
minor fixes and improvements of AsyncQueue, additional tests
cin
parents:
121
diff
changeset
|
57 Console.WriteLine("done: {0} ms, summ#1: {1}, summ#2: {2}, total: {3}, count: {4}", Environment.TickCount - t1, res1, res2, res1 + res2, count*2); |
121 | 58 |
103 | 59 var t2 = Environment.TickCount; |
60 Console.WriteLine("MTQueue: {0} ms", t2 - t1); | |
61 | |
62 t1 = Environment.TickCount; | |
93 | 63 |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
64 for (var i = 0; i < count * 2; i++) |
103 | 65 q2.Enqueue(i); |
66 | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
67 for (var i = 0; i < count * 2; i++) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
68 q2.Dequeue(); |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
69 |
103 | 70 t2 = Environment.TickCount; |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
71 Console.WriteLine("Queue: {0} ms", t2 - t1); |
103 | 72 |
104 | 73 q2 = new Queue<int>(); |
93 | 74 |
103 | 75 t1 = Environment.TickCount; |
76 | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
77 |
121 | 78 AsyncPool.RunThread( |
120 | 79 () => { |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
80 for (var i = 0; i < count; i++) |
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 q2.Enqueue(i); |
120 | 83 }, |
84 () => { | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
85 for (var i = 0; i < count; i++) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
86 lock (q2) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
87 q2.Enqueue(i); |
120 | 88 }, |
89 () => { | |
119
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 } |
108 | 97 |
120 | 98 }, |
99 () => { | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
100 for (int i = 0; i < count ;) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
101 lock (q2) { |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
102 if (q2.Count == 0) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
103 continue; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
104 q2.Dequeue(); |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
105 i++; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
106 } |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
107 |
120 | 108 } |
109 ) | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
110 .Combine() |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
111 .Join(); |
108 | 112 |
113 | |
103 | 114 |
115 t2 = Environment.TickCount; | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
116 Console.WriteLine("Queue+Lock: {0} ms", t2 - t1); |
93 | 117 |
118 } | |
119 } | |
120 } |