comparison 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
comparison
equal deleted inserted replaced
118:e046a94eecb1 119:2573b562e328
9 class MainClass { 9 class MainClass {
10 public static void Main(string[] args) { 10 public static void Main(string[] args) {
11 if (args == null) 11 if (args == null)
12 throw new ArgumentNullException("args"); 12 throw new ArgumentNullException("args");
13 13
14 var q1 = new MTQueue<int>(); 14 var q1 = new AsyncQueue<int>();
15 var q2 = new Queue<int>(); 15 var q2 = new Queue<int>();
16 16
17 const int count = 10000000; 17 const int count = 10000000;
18 18
19 19
20 var t1 = Environment.TickCount; 20 var t1 = Environment.TickCount;
21 21
22 Promise<int>.CreateComposite( 22 new [] {
23 new [] { 23 AsyncPool.InvokeNewThread(() => {
24 AsyncPool.InvokeNewThread(() => { 24 for (var i = 0; i < count; i++)
25 for (var i = 0; i < count; i++) 25 q1.Enqueue(i);
26 q1.Enqueue(i); 26 }),
27 }), 27 AsyncPool.InvokeNewThread(() => {
28 AsyncPool.InvokeNewThread(() => { 28 for (var i = 0; i < count; i++)
29 int temp = 0; 29 q1.Enqueue(i);
30 for(int i =0 ; i< count ; i++) 30 }),
31 while(!q1.TryDequeue(out temp)){ 31 AsyncPool.InvokeNewThread(() => {
32 } 32 int temp = 0;
33 }) 33 int i = 0;
34 } 34 while (i < count)
35 ).Join(); 35 if (q1.TryDequeue(out temp))
36 i++;
37 }),
38 AsyncPool.InvokeNewThread(() => {
39 int temp = 0;
40 int i = 0;
41 while (i < count)
42 if (q1.TryDequeue(out temp))
43 i++;
44 })
45 }
46 .Combine()
47 .Join();
36 48
37 var t2 = Environment.TickCount; 49 var t2 = Environment.TickCount;
38 Console.WriteLine("MTQueue: {0} ms", t2 - t1); 50 Console.WriteLine("MTQueue: {0} ms", t2 - t1);
39 51
40 t1 = Environment.TickCount; 52 t1 = Environment.TickCount;
41 53
42 for (var i = 0; i < count; i++) 54 for (var i = 0; i < count * 2; i++)
43 q2.Enqueue(i); 55 q2.Enqueue(i);
44 56
57 for (var i = 0; i < count * 2; i++)
58 q2.Dequeue();
59
45 t2 = Environment.TickCount; 60 t2 = Environment.TickCount;
46 Console.WriteLine("LinkedList: {0} ms", t2 - t1); 61 Console.WriteLine("Queue: {0} ms", t2 - t1);
47 62
48 q2 = new Queue<int>(); 63 q2 = new Queue<int>();
49 64
50 t1 = Environment.TickCount; 65 t1 = Environment.TickCount;
51 66
52 Promise<int>.CreateComposite( 67
53 new [] { 68 new [] {
54 AsyncPool.InvokeNewThread(() => { 69 AsyncPool.InvokeNewThread(() => {
55 for (var i = 0; i < count; i++) 70 for (var i = 0; i < count; i++)
56 lock (q2) 71 lock (q2)
57 q2.Enqueue(i); 72 q2.Enqueue(i);
58 }), 73 }),
59 AsyncPool.InvokeNewThread(() => { 74 AsyncPool.InvokeNewThread(() => {
60 for(int i = 0 ; i< count ;) 75 for (var i = 0; i < count; i++)
61 lock(q2) { 76 lock (q2)
62 if(q2.Count == 0) 77 q2.Enqueue(i);
63 continue; 78 }),
64 q2.Dequeue(); 79 AsyncPool.InvokeNewThread(() => {
65 i++; 80 for (int i = 0; i < count ;)
66 } 81 lock (q2) {
82 if (q2.Count == 0)
83 continue;
84 q2.Dequeue();
85 i++;
86 }
67 87
68 }) 88 }),
69 } 89 AsyncPool.InvokeNewThread(() => {
70 ).Join(); 90 for (int i = 0; i < count ;)
91 lock (q2) {
92 if (q2.Count == 0)
93 continue;
94 q2.Dequeue();
95 i++;
96 }
97
98 })
99 }
100 .Combine()
101 .Join();
71 102
72 103
73 104
74 t2 = Environment.TickCount; 105 t2 = Environment.TickCount;
75 Console.WriteLine("LinkedList+Lock: {0} ms", t2 - t1); 106 Console.WriteLine("Queue+Lock: {0} ms", t2 - t1);
76 107
77 } 108 }
78 } 109 }
79 } 110 }