Mercurial > pub > ImplabNet
annotate MonoPlay/Program.cs @ 120:f1b897999260 v2
improved asyncpool usability
working on batch operations on asyncqueue
author | cin |
---|---|
date | Mon, 12 Jan 2015 05:19:52 +0300 |
parents | 2573b562e328 |
children | 62d2f1e98c4e |
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 |
120 | 22 AsyncPool.ThreadRun( |
23 () => { | |
119
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); |
120 | 26 }, |
27 () => { | |
119
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); |
120 | 30 }, |
31 () => { | |
119
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++; |
120 | 37 }, |
38 () => { | |
119
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++; |
120 | 44 } |
45 ) | |
119
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 |
120 | 68 AsyncPool.ThreadRun( |
69 () => { | |
119
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); |
120 | 73 }, |
74 () => { | |
119
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); |
120 | 78 }, |
79 () => { | |
119
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 |
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 } |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
108
diff
changeset
|
97 |
120 | 98 } |
99 ) | |
119
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 } |