comparison Implab.Playground/Program.cs @ 255:b00441e04738 v3

Adde workaround to the behaviour of the logical operations stack in conjuction with async/await methods
author cin
date Wed, 04 Apr 2018 15:38:48 +0300
parents 302ca905c19e
children 547a2fc0d93e
comparison
equal deleted inserted replaced
254:12c00235b105 255:b00441e04738
1 using Implab.Formats.Json; 1 using Implab.Diagnostics;
2 using Implab.Formats.Json;
2 using Implab.Parallels; 3 using Implab.Parallels;
3 using Implab.Xml; 4 using Implab.Xml;
4 using System; 5 using System;
5 using System.Collections.Concurrent; 6 using System.Collections.Concurrent;
6 using System.Collections.Generic; 7 using System.Collections.Generic;
11 using System.Threading.Tasks; 12 using System.Threading.Tasks;
12 using System.Xml; 13 using System.Xml;
13 using System.Xml.Serialization; 14 using System.Xml.Serialization;
14 15
15 namespace Implab.Playground { 16 namespace Implab.Playground {
17 using System.Diagnostics;
18 using System.Runtime.Remoting.Messaging;
19 using static Trace<Program>;
20
16 public class Program { 21 public class Program {
17 22
18 static void EnqueueRange<T>(ConcurrentQueue<T> q, T[] data, int offset, int len) { 23 static void Main(string[] args) {
19 for (var i = offset; i < offset + len; i++) 24 var listener = new SimpleTraceListener(Console.Out);
20 q.Enqueue(data[i]); 25
26 var source = Trace<Program>.TraceSource;
27 source.Switch.Level = SourceLevels.All;
28
29 source.Listeners.Add(listener);
30
31 var t = Environment.TickCount;
32
33 Main().Wait();
34
35 Console.WriteLine($"Done: {Environment.TickCount - t} ms");
36 Console.ReadKey();
21 } 37 }
22 38
23 static bool TryDequeueRange<T>(ConcurrentQueue<T> q,T[] buffer,int offset, int len, out int actual) { 39 static async Task Main() {
24 actual = 0; 40 using (LogicalOperation(nameof(Main))) {
25 T res; 41 Log("Start");
26 while(q.TryDequeue(out res)) { 42 await SomeAsync();
27 buffer[offset + actual] = res; 43 Log("End");
28 actual++;
29 if (actual == len)
30 break;
31 } 44 }
32 return actual != 0;
33 } 45 }
34 46
35 static void EnqueueRange<T>(SimpleAsyncQueue<T> q, T[] data, int offset, int len) { 47 static async Task SomeAsync() {
36 for (var i = offset; i < offset + len; i++) 48 using (LogicalOperation(nameof(SomeAsync))) {
37 q.Enqueue(data[i]); 49 Log("Do prepare");
38 } 50 await Task.Yield();
39 51 Log("Yield");
40 static bool TryDequeueRange<T>(SimpleAsyncQueue<T> q, T[] buffer, int offset, int len, out int actual) {
41 actual = 0;
42 T res;
43 while (q.TryDequeue(out res)) {
44 buffer[offset + actual] = res;
45 actual++;
46 if (actual == len)
47 break;
48 } 52 }
49 return actual != 0;
50 }
51
52 static void EnqueueRange<T>(AsyncQueue<T> q, T[] data, int offset, int len) {
53 for (var i = offset; i < offset + len; i++)
54 q.Enqueue(data[i]);
55 }
56
57 static bool TryDequeueRange<T>(AsyncQueue<T> q, T[] buffer, int offset, int len, out int actual) {
58 actual = 0;
59 T res;
60 while (q.TryDequeue(out res)) {
61 buffer[offset + actual] = res;
62 actual++;
63 if (actual == len)
64 break;
65 }
66 return actual != 0;
67 }
68
69
70 /*static void EnqueueRange<T>(AsyncQueue<T> q, T[] data, int offset, int len) {
71 q.EnqueueRange(data, offset, len);
72 }
73
74 static bool TryDequeueRange<T>(AsyncQueue<T> q, T[] buffer, int offset, int len, out int actual) {
75 return q.TryDequeueRange(buffer, offset, len, out actual);
76 }*/
77
78
79 static void Main(string[] args) {
80
81 var t = Environment.TickCount;
82 using (var reader = JsonReader.Create("e:\\citylots.json")) {
83 while (reader.Read()) {
84 }
85 }
86
87 Console.WriteLine($"JsonReader: {Environment.TickCount - t} ms");
88
89 Console.WriteLine("done");
90 } 53 }
91 } 54 }
92 } 55 }