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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
1 using System;
dc4942d09e74 improved tracing
cin
parents:
diff changeset
2 using Implab.Diagnostics;
dc4942d09e74 improved tracing
cin
parents:
diff changeset
3 using Implab.Parallels;
dc4942d09e74 improved tracing
cin
parents:
diff changeset
4 using Implab;
103
cin
parents: 94
diff changeset
5 using System.Collections.Generic;
cin
parents: 94
diff changeset
6 using System.Collections.Concurrent;
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
7
dc4942d09e74 improved tracing
cin
parents:
diff changeset
8 namespace MonoPlay {
dc4942d09e74 improved tracing
cin
parents:
diff changeset
9 class MainClass {
dc4942d09e74 improved tracing
cin
parents:
diff changeset
10 public static void Main(string[] args) {
94
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
11 if (args == null)
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
12 throw new ArgumentNullException("args");
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
13
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 108
diff changeset
14 var q1 = new AsyncQueue<int>();
104
5f10d54b45df renamed Promise.Last -> Promise.On
cin
parents: 103
diff changeset
15 var q2 = new Queue<int>();
103
cin
parents: 94
diff changeset
16
cin
parents: 94
diff changeset
17 const int count = 10000000;
cin
parents: 94
diff changeset
18
108
cin
parents: 104
diff changeset
19
103
cin
parents: 94
diff changeset
20 var t1 = Environment.TickCount;
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
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
dc4942d09e74 improved tracing
cin
parents:
diff changeset
48
103
cin
parents: 94
diff changeset
49 var t2 = Environment.TickCount;
cin
parents: 94
diff changeset
50 Console.WriteLine("MTQueue: {0} ms", t2 - t1);
cin
parents: 94
diff changeset
51
cin
parents: 94
diff changeset
52 t1 = Environment.TickCount;
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
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
cin
parents: 94
diff changeset
55 q2.Enqueue(i);
cin
parents: 94
diff changeset
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
cin
parents: 94
diff changeset
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
cin
parents: 94
diff changeset
62
104
5f10d54b45df renamed Promise.Last -> Promise.On
cin
parents: 103
diff changeset
63 q2 = new Queue<int>();
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
64
103
cin
parents: 94
diff changeset
65 t1 = Environment.TickCount;
cin
parents: 94
diff changeset
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
cin
parents: 104
diff changeset
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
cin
parents: 104
diff changeset
102
cin
parents: 104
diff changeset
103
103
cin
parents: 94
diff changeset
104
cin
parents: 94
diff changeset
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
dc4942d09e74 improved tracing
cin
parents:
diff changeset
107
dc4942d09e74 improved tracing
cin
parents:
diff changeset
108 }
dc4942d09e74 improved tracing
cin
parents:
diff changeset
109 }
dc4942d09e74 improved tracing
cin
parents:
diff changeset
110 }