annotate MonoPlay/Program.cs @ 116:da56ba7b1aab v2

minor refactoring
author cin
date Tue, 23 Dec 2014 03:20:39 +0300
parents f3bdb7ba59b9
children 2573b562e328
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
103
cin
parents: 94
diff changeset
14 var q1 = new MTQueue<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
108
cin
parents: 104
diff changeset
22 Promise<int>.CreateComposite(
cin
parents: 104
diff changeset
23 new [] {
cin
parents: 104
diff changeset
24 AsyncPool.InvokeNewThread(() => {
cin
parents: 104
diff changeset
25 for (var i = 0; i < count; i++)
cin
parents: 104
diff changeset
26 q1.Enqueue(i);
cin
parents: 104
diff changeset
27 }),
cin
parents: 104
diff changeset
28 AsyncPool.InvokeNewThread(() => {
cin
parents: 104
diff changeset
29 int temp = 0;
cin
parents: 104
diff changeset
30 for(int i =0 ; i< count ; i++)
cin
parents: 104
diff changeset
31 while(!q1.TryDequeue(out temp)){
cin
parents: 104
diff changeset
32 }
cin
parents: 104
diff changeset
33 })
cin
parents: 104
diff changeset
34 }
cin
parents: 104
diff changeset
35 ).Join();
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
36
103
cin
parents: 94
diff changeset
37 var t2 = Environment.TickCount;
cin
parents: 94
diff changeset
38 Console.WriteLine("MTQueue: {0} ms", t2 - t1);
cin
parents: 94
diff changeset
39
cin
parents: 94
diff changeset
40 t1 = Environment.TickCount;
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
41
103
cin
parents: 94
diff changeset
42 for (var i = 0; i < count; i++)
cin
parents: 94
diff changeset
43 q2.Enqueue(i);
cin
parents: 94
diff changeset
44
cin
parents: 94
diff changeset
45 t2 = Environment.TickCount;
cin
parents: 94
diff changeset
46 Console.WriteLine("LinkedList: {0} ms", t2 - t1);
cin
parents: 94
diff changeset
47
104
5f10d54b45df renamed Promise.Last -> Promise.On
cin
parents: 103
diff changeset
48 q2 = new Queue<int>();
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
49
103
cin
parents: 94
diff changeset
50 t1 = Environment.TickCount;
cin
parents: 94
diff changeset
51
108
cin
parents: 104
diff changeset
52 Promise<int>.CreateComposite(
cin
parents: 104
diff changeset
53 new [] {
cin
parents: 104
diff changeset
54 AsyncPool.InvokeNewThread(() => {
cin
parents: 104
diff changeset
55 for (var i = 0; i < count; i++)
cin
parents: 104
diff changeset
56 lock (q2)
cin
parents: 104
diff changeset
57 q2.Enqueue(i);
cin
parents: 104
diff changeset
58 }),
cin
parents: 104
diff changeset
59 AsyncPool.InvokeNewThread(() => {
cin
parents: 104
diff changeset
60 for(int i = 0 ; i< count ;)
cin
parents: 104
diff changeset
61 lock(q2) {
cin
parents: 104
diff changeset
62 if(q2.Count == 0)
cin
parents: 104
diff changeset
63 continue;
cin
parents: 104
diff changeset
64 q2.Dequeue();
cin
parents: 104
diff changeset
65 i++;
cin
parents: 104
diff changeset
66 }
cin
parents: 104
diff changeset
67
cin
parents: 104
diff changeset
68 })
cin
parents: 104
diff changeset
69 }
cin
parents: 104
diff changeset
70 ).Join();
cin
parents: 104
diff changeset
71
cin
parents: 104
diff changeset
72
103
cin
parents: 94
diff changeset
73
cin
parents: 94
diff changeset
74 t2 = Environment.TickCount;
cin
parents: 94
diff changeset
75 Console.WriteLine("LinkedList+Lock: {0} ms", t2 - t1);
93
dc4942d09e74 improved tracing
cin
parents:
diff changeset
76
dc4942d09e74 improved tracing
cin
parents:
diff changeset
77 }
dc4942d09e74 improved tracing
cin
parents:
diff changeset
78 }
dc4942d09e74 improved tracing
cin
parents:
diff changeset
79 }