diff 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
line wrap: on
line diff
--- a/MonoPlay/Program.cs	Sun Dec 28 16:09:03 2014 +0300
+++ b/MonoPlay/Program.cs	Sun Jan 11 19:13:02 2015 +0300
@@ -11,7 +11,7 @@
             if (args == null)
                 throw new ArgumentNullException("args");
 
-            var q1 = new MTQueue<int>();
+            var q1 = new AsyncQueue<int>();
             var q2 = new Queue<int>();
 
             const int count = 10000000;
@@ -19,60 +19,91 @@
 
             var t1 = Environment.TickCount;
 
-            Promise<int>.CreateComposite(
-                new [] {
-                    AsyncPool.InvokeNewThread(() => {
-                        for (var i = 0; i < count; i++)
-                            q1.Enqueue(i);
-                    }),
-                    AsyncPool.InvokeNewThread(() => {
-                        int temp = 0;
-                        for(int i =0 ; i< count ; i++)
-                            while(!q1.TryDequeue(out temp)){
-                            }
-                    })
-                }
-            ).Join();
+            new [] {
+                AsyncPool.InvokeNewThread(() => {
+                    for (var i = 0; i < count; i++)
+                        q1.Enqueue(i);
+                }),
+                AsyncPool.InvokeNewThread(() => {
+                    for (var i = 0; i < count; i++)
+                        q1.Enqueue(i);
+                }),
+                AsyncPool.InvokeNewThread(() => {
+                    int temp = 0;
+                    int i = 0;
+                    while (i < count)
+                        if (q1.TryDequeue(out temp))
+                            i++;
+                }),
+                AsyncPool.InvokeNewThread(() => {
+                    int temp = 0;
+                    int i = 0;
+                    while (i < count)
+                        if (q1.TryDequeue(out temp))
+                            i++;
+                })
+            }
+                .Combine()
+                .Join();
 
             var t2 = Environment.TickCount;
             Console.WriteLine("MTQueue: {0} ms", t2 - t1);
 
             t1 = Environment.TickCount;
 
-            for (var i = 0; i < count; i++)
+            for (var i = 0; i < count * 2; i++)
                 q2.Enqueue(i);
 
+            for (var i = 0; i < count * 2; i++)
+                q2.Dequeue();
+
             t2 = Environment.TickCount;
-            Console.WriteLine("LinkedList: {0} ms", t2 - t1);
+            Console.WriteLine("Queue: {0} ms", t2 - t1);
 
             q2 = new Queue<int>();
 
             t1 = Environment.TickCount;
 
-            Promise<int>.CreateComposite(
-                new [] {
-                    AsyncPool.InvokeNewThread(() => {
-                        for (var i = 0; i < count; i++)
-                            lock (q2)
-                                q2.Enqueue(i);
-                    }),
-                    AsyncPool.InvokeNewThread(() => {
-                        for(int i = 0 ; i< count ;)
-                            lock(q2) {
-                                if(q2.Count == 0)
-                                    continue;
-                                q2.Dequeue();
-                                i++;
-                            }
+         
+            new [] {
+                AsyncPool.InvokeNewThread(() => {
+                    for (var i = 0; i < count; i++)
+                        lock (q2)
+                            q2.Enqueue(i);
+                }),
+                AsyncPool.InvokeNewThread(() => {
+                    for (var i = 0; i < count; i++)
+                        lock (q2)
+                            q2.Enqueue(i);
+                }),
+                AsyncPool.InvokeNewThread(() => {
+                    for (int i = 0; i < count ;)
+                        lock (q2) {
+                            if (q2.Count == 0)
+                                continue;
+                            q2.Dequeue();
+                            i++;
+                        }
 
-                    })
-                }
-            ).Join();
+                }),
+                AsyncPool.InvokeNewThread(() => {
+                    for (int i = 0; i < count ;)
+                        lock (q2) {
+                            if (q2.Count == 0)
+                                continue;
+                            q2.Dequeue();
+                            i++;
+                        }
+
+                })
+            }
+                .Combine()
+                .Join();
 
 
 
             t2 = Environment.TickCount;
-            Console.WriteLine("LinkedList+Lock: {0} ms", t2 - t1);
+            Console.WriteLine("Queue+Lock: {0} ms", t2 - t1);
 
         }
     }