Program.cs
120 lines
| 3.6 KiB
| text/x-csharp
|
CSharpLexer
/ MonoPlay / Program.cs
cin
|
r93 | using System; | ||
using Implab.Diagnostics; | ||||
using Implab.Parallels; | ||||
using Implab; | ||||
cin
|
r103 | using System.Collections.Generic; | ||
using System.Collections.Concurrent; | ||||
cin
|
r93 | |||
namespace MonoPlay { | ||||
class MainClass { | ||||
public static void Main(string[] args) { | ||||
cin
|
r94 | if (args == null) | ||
throw new ArgumentNullException("args"); | ||||
cin
|
r119 | var q1 = new AsyncQueue<int>(); | ||
cin
|
r104 | var q2 = new Queue<int>(); | ||
cin
|
r103 | |||
const int count = 10000000; | ||||
cin
|
r121 | int res1 = 0, res2 = 0; | ||
cin
|
r103 | var t1 = Environment.TickCount; | ||
cin
|
r93 | |||
cin
|
r121 | AsyncPool.RunThread( | ||
cin
|
r120 | () => { | ||
cin
|
r119 | for (var i = 0; i < count; i++) | ||
cin
|
r121 | q1.Enqueue(1); | ||
Console.WriteLine("done writer #1: {0} ms", Environment.TickCount - t1); | ||||
cin
|
r120 | }, | ||
() => { | ||||
cin
|
r119 | for (var i = 0; i < count; i++) | ||
cin
|
r121 | q1.Enqueue(2); | ||
Console.WriteLine("done writer #2: {0} ms", Environment.TickCount - t1); | ||||
cin
|
r120 | }, | ||
() => { | ||||
cin
|
r119 | int temp = 0; | ||
int i = 0; | ||||
while (i < count) | ||||
cin
|
r121 | if (q1.TryDequeue(out temp)) { | ||
cin
|
r119 | i++; | ||
cin
|
r121 | res1 += temp; | ||
} | ||||
Console.WriteLine("done reader #1: {0} ms", Environment.TickCount - t1); | ||||
cin
|
r120 | }, | ||
() => { | ||||
cin
|
r119 | int temp = 0; | ||
int i = 0; | ||||
while (i < count) | ||||
cin
|
r121 | if (q1.TryDequeue(out temp)) { | ||
cin
|
r119 | i++; | ||
cin
|
r121 | res2 += temp; | ||
} | ||||
Console.WriteLine("done reader #2: {0} ms", Environment.TickCount - t1); | ||||
cin
|
r120 | } | ||
) | ||||
cin
|
r119 | .Combine() | ||
.Join(); | ||||
cin
|
r93 | |||
cin
|
r122 | Console.WriteLine("done: {0} ms, summ#1: {1}, summ#2: {2}, total: {3}, count: {4}", Environment.TickCount - t1, res1, res2, res1 + res2, count*2); | ||
cin
|
r121 | |||
cin
|
r103 | var t2 = Environment.TickCount; | ||
Console.WriteLine("MTQueue: {0} ms", t2 - t1); | ||||
t1 = Environment.TickCount; | ||||
cin
|
r93 | |||
cin
|
r119 | for (var i = 0; i < count * 2; i++) | ||
cin
|
r103 | q2.Enqueue(i); | ||
cin
|
r119 | for (var i = 0; i < count * 2; i++) | ||
q2.Dequeue(); | ||||
cin
|
r103 | t2 = Environment.TickCount; | ||
cin
|
r119 | Console.WriteLine("Queue: {0} ms", t2 - t1); | ||
cin
|
r103 | |||
cin
|
r104 | q2 = new Queue<int>(); | ||
cin
|
r93 | |||
cin
|
r103 | t1 = Environment.TickCount; | ||
cin
|
r119 | |||
cin
|
r121 | AsyncPool.RunThread( | ||
cin
|
r120 | () => { | ||
cin
|
r119 | for (var i = 0; i < count; i++) | ||
lock (q2) | ||||
q2.Enqueue(i); | ||||
cin
|
r120 | }, | ||
() => { | ||||
cin
|
r119 | for (var i = 0; i < count; i++) | ||
lock (q2) | ||||
q2.Enqueue(i); | ||||
cin
|
r120 | }, | ||
() => { | ||||
cin
|
r119 | for (int i = 0; i < count ;) | ||
lock (q2) { | ||||
if (q2.Count == 0) | ||||
continue; | ||||
q2.Dequeue(); | ||||
i++; | ||||
} | ||||
cin
|
r108 | |||
cin
|
r120 | }, | ||
() => { | ||||
cin
|
r119 | for (int i = 0; i < count ;) | ||
lock (q2) { | ||||
if (q2.Count == 0) | ||||
continue; | ||||
q2.Dequeue(); | ||||
i++; | ||||
} | ||||
cin
|
r120 | } | ||
) | ||||
cin
|
r119 | .Combine() | ||
.Join(); | ||||
cin
|
r108 | |||
cin
|
r103 | |||
t2 = Environment.TickCount; | ||||
cin
|
r119 | Console.WriteLine("Queue+Lock: {0} ms", t2 - t1); | ||
cin
|
r93 | |||
} | ||||
} | ||||
} | ||||