1 | using System.Threading.Tasks;
|
---|
2 |
|
---|
3 | namespace TestPooling {
|
---|
4 | using System;
|
---|
5 | using System.Collections.Generic;
|
---|
6 | using System.Diagnostics;
|
---|
7 | using System.Linq;
|
---|
8 | using System.Text;
|
---|
9 | using System.Threading;
|
---|
10 | using DirtyList;
|
---|
11 | using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
|
---|
12 | using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;
|
---|
13 | using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
|
---|
14 | using HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator;
|
---|
15 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
16 | using HeuristicLab.Problems.ProgramSynthesis.Push.Parser;
|
---|
17 | using HeuristicLab.Problems.ProgramSynthesis.Push.Simplifier;
|
---|
18 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
19 | using HeuristicLab.Random;
|
---|
20 |
|
---|
21 | class Program {
|
---|
22 | private const int PartitionSize = 512;
|
---|
23 | private const int ExampleCount = 200;
|
---|
24 | private const int InstanceCount = 1024;
|
---|
25 | private const int PopulationCount = 100;
|
---|
26 |
|
---|
27 | private static int TotalInstanceCount => PopulationCount * InstanceCount * ExampleCount;
|
---|
28 |
|
---|
29 | static void Main(string[] args) {
|
---|
30 | //Test1();
|
---|
31 | //Test1_2();
|
---|
32 | //Test3();
|
---|
33 |
|
---|
34 | //TestCollectionPooling();
|
---|
35 |
|
---|
36 | //DirtyListTest();
|
---|
37 |
|
---|
38 | //TestAddRangeNConcat();
|
---|
39 |
|
---|
40 | //TestStacks();
|
---|
41 |
|
---|
42 | //SimpleTest();
|
---|
43 | //Stepwise().Wait();
|
---|
44 | //PerformanceTestInterpreter();
|
---|
45 | //PerformanceParallelTestInterpreter();
|
---|
46 | //PerformanceTestLinearCodeGenerator();
|
---|
47 | //TestRobustness();
|
---|
48 | //TestPool();
|
---|
49 | //TestPoolPerformance();
|
---|
50 | //TestSimplifier();
|
---|
51 | //FlagsTest();
|
---|
52 | //TestExpressionDependencies();
|
---|
53 | //TestSplitLogic();
|
---|
54 | //TestStringBuilder();
|
---|
55 | TestRandomReset();
|
---|
56 |
|
---|
57 | Console.WriteLine("\nPress any key to continue...");
|
---|
58 | Console.ReadKey(false);
|
---|
59 | }
|
---|
60 |
|
---|
61 | private static void TestStringBuilder() {
|
---|
62 | var sb = new StringBuilder();
|
---|
63 | sb.Append("test");
|
---|
64 |
|
---|
65 | var s1 = sb.ToString();
|
---|
66 | var s2 = sb.ToString();
|
---|
67 |
|
---|
68 | // false
|
---|
69 | var equal = ReferenceEquals(s1, s2);
|
---|
70 | }
|
---|
71 |
|
---|
72 | private static void TestRandomReset() {
|
---|
73 | var random = new MersenneTwister(1000);
|
---|
74 |
|
---|
75 | var x = random.Next();
|
---|
76 | random.Reset();
|
---|
77 | var y = random.Next();
|
---|
78 |
|
---|
79 | if (x != y) {
|
---|
80 | throw new InvalidOperationException();
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | private static void TestSplitLogic() {
|
---|
85 | var sw = new Stopwatch();
|
---|
86 | var iMax = 100000000u;
|
---|
87 | var repeats = 10;
|
---|
88 | var or = new BooleanOrExpression();
|
---|
89 | var and = new BooleanAndExpression();
|
---|
90 | var ors = new Expression[iMax];
|
---|
91 | var ands = new Expression[iMax];
|
---|
92 | var booleans = new bool[iMax];
|
---|
93 |
|
---|
94 | for (var i = 0u; i < iMax; i++) {
|
---|
95 | ors[i] = or;
|
---|
96 | ands[i] = and;
|
---|
97 | booleans[i] = i % 2 == 0;
|
---|
98 | }
|
---|
99 |
|
---|
100 | var orsProgram = new PushProgram(ors);
|
---|
101 | var andsProgram = new PushProgram(ands);
|
---|
102 |
|
---|
103 | using (var interpreter = new PushInterpreter()) {
|
---|
104 | for (var i = 0u; i < repeats; i++) {
|
---|
105 | interpreter.BooleanStack.Push(booleans);
|
---|
106 |
|
---|
107 | sw.Start();
|
---|
108 | interpreter.Run(orsProgram);
|
---|
109 | sw.Stop();
|
---|
110 | interpreter.Reset();
|
---|
111 | }
|
---|
112 |
|
---|
113 | Console.WriteLine("ors:\t" + sw.ElapsedTicks / repeats + " - " + sw.ElapsedMilliseconds);
|
---|
114 |
|
---|
115 | sw.Reset();
|
---|
116 | interpreter.Reset();
|
---|
117 |
|
---|
118 | for (var i = 0u; i < repeats; i++) {
|
---|
119 | interpreter.BooleanStack.Push(booleans);
|
---|
120 |
|
---|
121 | sw.Start();
|
---|
122 | interpreter.Run(andsProgram);
|
---|
123 | sw.Stop();
|
---|
124 | interpreter.Reset();
|
---|
125 | }
|
---|
126 |
|
---|
127 | Console.WriteLine("ands:\t" + sw.ElapsedTicks / repeats + " - " + sw.ElapsedMilliseconds);
|
---|
128 |
|
---|
129 | sw.Reset();
|
---|
130 | interpreter.Reset();
|
---|
131 |
|
---|
132 | for (var i = 0u; i < repeats; i++) {
|
---|
133 | interpreter.BooleanStack.Push(booleans);
|
---|
134 |
|
---|
135 | sw.Start();
|
---|
136 | interpreter.Run(orsProgram);
|
---|
137 | sw.Stop();
|
---|
138 | interpreter.Reset();
|
---|
139 | }
|
---|
140 |
|
---|
141 | Console.WriteLine("ors:\t" + sw.ElapsedTicks / repeats + " - " + sw.ElapsedMilliseconds);
|
---|
142 |
|
---|
143 | sw.Reset();
|
---|
144 | interpreter.Reset();
|
---|
145 |
|
---|
146 | for (var i = 0u; i < repeats; i++) {
|
---|
147 | interpreter.BooleanStack.Push(booleans);
|
---|
148 |
|
---|
149 | sw.Start();
|
---|
150 | interpreter.Run(andsProgram);
|
---|
151 | sw.Stop();
|
---|
152 | interpreter.Reset();
|
---|
153 | }
|
---|
154 |
|
---|
155 | Console.WriteLine("ands:\t" + sw.ElapsedTicks / repeats + " - " + sw.ElapsedMilliseconds);
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | private static void TestExpressionDependencies() {
|
---|
160 | var enabledExpressions = ExpressionTable.GetExpressionsByStackTypes(StackTypes.Boolean | StackTypes.Integer);
|
---|
161 | }
|
---|
162 |
|
---|
163 | private static void FlagsTest() {
|
---|
164 | var types = StackTypes.Boolean | StackTypes.Char | StackTypes.Integer;
|
---|
165 |
|
---|
166 | StackTypes test1 = (StackTypes)(StackTypes.Boolean - types);
|
---|
167 | StackTypes test2 = (StackTypes)((StackTypes.Boolean | StackTypes.Float) - types);
|
---|
168 | StackTypes test3 = ((StackTypes.Boolean | StackTypes.Float) & types);
|
---|
169 | StackTypes test4 = ((StackTypes.Boolean | StackTypes.Float) ^ types);
|
---|
170 | StackTypes test5 = ((StackTypes.Boolean | StackTypes.Float) | types);
|
---|
171 | StackTypes test6 = (StackTypes.Boolean | StackTypes.Float) & ~types;
|
---|
172 | StackTypes test7 = StackTypes.Boolean & ~types;
|
---|
173 | }
|
---|
174 |
|
---|
175 | private static void TestStacks() {
|
---|
176 | var sw = new Stopwatch();
|
---|
177 | var stack = new DirtyList.PushStack<int>();
|
---|
178 | var stack2 = new PushStack2<int>();
|
---|
179 | var stack3 = new PushStack3<int>();
|
---|
180 | var listC = new List<int>(1000);
|
---|
181 | var count = 200000;
|
---|
182 | var repeats = 10;
|
---|
183 |
|
---|
184 | for (var i = 0; i < 100; i++) {
|
---|
185 | listC.Add(i);
|
---|
186 | }
|
---|
187 |
|
---|
188 | sw.Start();
|
---|
189 | for (var j = 0; j < repeats; j++) {
|
---|
190 | stack.Clear();
|
---|
191 |
|
---|
192 | for (int i = 0; i < count; i++) {
|
---|
193 | stack.Push(listC);
|
---|
194 | }
|
---|
195 | }
|
---|
196 | sw.Stop();
|
---|
197 |
|
---|
198 | Console.WriteLine("{0}", sw.ElapsedMilliseconds / repeats);
|
---|
199 |
|
---|
200 | sw.Restart();
|
---|
201 | for (var j = 0; j < repeats; j++) {
|
---|
202 | stack2.Clear();
|
---|
203 |
|
---|
204 | for (int i = 0; i < count; i++) {
|
---|
205 | stack2.PushRange(listC);
|
---|
206 | }
|
---|
207 | }
|
---|
208 | sw.Stop();
|
---|
209 |
|
---|
210 | Console.WriteLine("{0}", sw.ElapsedMilliseconds / repeats);
|
---|
211 |
|
---|
212 | sw.Restart();
|
---|
213 | for (var j = 0; j < repeats; j++) {
|
---|
214 | stack3.Clear();
|
---|
215 |
|
---|
216 | for (int i = 0; i < count; i++) {
|
---|
217 | stack3.PushRange(listC);
|
---|
218 | }
|
---|
219 | }
|
---|
220 | sw.Stop();
|
---|
221 |
|
---|
222 | Console.WriteLine("{0}", sw.ElapsedMilliseconds / repeats);
|
---|
223 |
|
---|
224 | sw.Restart();
|
---|
225 | for (int i = 0; i < count * listC.Count; i++) {
|
---|
226 | stack.Pop();
|
---|
227 | }
|
---|
228 |
|
---|
229 | Console.WriteLine("{0}", sw.ElapsedMilliseconds);
|
---|
230 |
|
---|
231 | sw.Restart();
|
---|
232 | for (int i = 0; i < count * listC.Count; i++) {
|
---|
233 | stack2.Pop();
|
---|
234 | }
|
---|
235 |
|
---|
236 | Console.WriteLine("{0}", sw.ElapsedMilliseconds);
|
---|
237 |
|
---|
238 |
|
---|
239 | }
|
---|
240 |
|
---|
241 | private static void TestAddRangeNConcat() {
|
---|
242 | var sw = new Stopwatch();
|
---|
243 | var count = 10000;
|
---|
244 | var listA = new List<int>();
|
---|
245 | var listB = new List<int>();
|
---|
246 | var listC = new List<int>(1000);
|
---|
247 |
|
---|
248 | for (var i = 0; i < 1000; i++) {
|
---|
249 | listC.Add(i);
|
---|
250 | }
|
---|
251 |
|
---|
252 | sw.Start();
|
---|
253 | for (int i = 0; i < count; i++) {
|
---|
254 | listA.AddRange(listC);
|
---|
255 | }
|
---|
256 | sw.Stop();
|
---|
257 |
|
---|
258 | Console.WriteLine("{0}", sw.ElapsedMilliseconds);
|
---|
259 |
|
---|
260 | sw.Restart();
|
---|
261 | for (int i = 0; i < count; i++) {
|
---|
262 | listB = listB.Concat(listC).ToList();
|
---|
263 | }
|
---|
264 | sw.Stop();
|
---|
265 |
|
---|
266 | Console.WriteLine("{0}", sw.ElapsedMilliseconds);
|
---|
267 |
|
---|
268 | }
|
---|
269 |
|
---|
270 | private static void DirtyListTest() {
|
---|
271 | var sw = new Stopwatch();
|
---|
272 | var provider = new ManagedPoolProvider<Person>(100, () => new Person());
|
---|
273 | var provider2 = new ManagedPoolProvider<Person>(100, () => new Person());
|
---|
274 | var dl = new DirtyList<Person>();
|
---|
275 | var l = new List<Person>();
|
---|
276 |
|
---|
277 | sw.Start();
|
---|
278 | for (var i = 0; i < 30000000; i++) {
|
---|
279 | using (var pool = provider.CreatePool()) {
|
---|
280 | for (var j = 0; j < 2; j++) {
|
---|
281 | var person = pool.Get();
|
---|
282 | person.Age = i + j;
|
---|
283 | person.Name = (i + j).ToString();
|
---|
284 |
|
---|
285 | dl.Add(person);
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | dl.Clear();
|
---|
290 | }
|
---|
291 | sw.Stop();
|
---|
292 |
|
---|
293 | Console.WriteLine("DirtyList: {0}", sw.Elapsed.TotalSeconds);
|
---|
294 |
|
---|
295 | sw.Restart();
|
---|
296 | for (var i = 0; i < 30000000; i++) {
|
---|
297 | using (var pool = provider2.CreatePool()) {
|
---|
298 | for (var j = 0; j < 2; j++) {
|
---|
299 | var person = pool.Get();
|
---|
300 | person.Age = i + j;
|
---|
301 | person.Name = (i + j).ToString();
|
---|
302 |
|
---|
303 | l.Add(person);
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | l.Clear();
|
---|
308 | }
|
---|
309 | sw.Stop();
|
---|
310 |
|
---|
311 | Console.WriteLine("List: {0}", sw.Elapsed.TotalSeconds);
|
---|
312 | }
|
---|
313 |
|
---|
314 | private static void TestCollectionPooling() {
|
---|
315 | var sw = new Stopwatch();
|
---|
316 | sw.Start();
|
---|
317 |
|
---|
318 | var provider = new ManagedPoolProvider<PooledList<Person>>(PartitionSize, () => new PooledList<Person>());
|
---|
319 |
|
---|
320 | Parallel.For(0, PopulationCount, pc => {
|
---|
321 | for (var e = 0; e < ExampleCount; e++) {
|
---|
322 | using (var pool = provider.CreatePool()) {
|
---|
323 | var list = pool.Get();
|
---|
324 |
|
---|
325 | for (var i = 0; i < InstanceCount; i++)
|
---|
326 | list.Add(new Person {
|
---|
327 | Age = pc + e + i,
|
---|
328 | Name = string.Format("{0} - {1} - {2}", pc, e, i)
|
---|
329 | });
|
---|
330 | }
|
---|
331 | }
|
---|
332 | });
|
---|
333 | sw.Stop();
|
---|
334 |
|
---|
335 | Console.WriteLine("Collection Pooling - Duration: {0} - {1} for {2}/{3} instances", sw.ElapsedTicks, sw.Elapsed.TotalSeconds, provider.InstanceCount, TotalInstanceCount);
|
---|
336 | }
|
---|
337 |
|
---|
338 | private static void Test1_2() {
|
---|
339 | var sw = new Stopwatch();
|
---|
340 | sw.Start();
|
---|
341 |
|
---|
342 | var provider = new ManagedPoolProvider<Person>(PartitionSize, () => new Person());
|
---|
343 |
|
---|
344 | Parallel.For(0, PopulationCount, pc => {
|
---|
345 | for (var e = 0; e < ExampleCount; e++) {
|
---|
346 | using (var pool = provider.CreatePool()) {
|
---|
347 | for (var i = 0; i < InstanceCount; i++) {
|
---|
348 | var person = pool.Get();
|
---|
349 | person.Age = pc + e + i;
|
---|
350 | person.Name = string.Format("{0} - {1} - {2}", pc, e, i);
|
---|
351 | }
|
---|
352 | }
|
---|
353 | }
|
---|
354 | });
|
---|
355 | sw.Stop();
|
---|
356 |
|
---|
357 | Console.WriteLine("1_2 - Duration: {0} - {1} for {2}/{3} instances", sw.ElapsedTicks, sw.Elapsed.TotalSeconds, provider.InstanceCount, TotalInstanceCount);
|
---|
358 | }
|
---|
359 |
|
---|
360 | private static void Test3() {
|
---|
361 | var count = 0;
|
---|
362 | var sw = new Stopwatch();
|
---|
363 |
|
---|
364 | sw.Start();
|
---|
365 | Parallel.For(0, PopulationCount, pc => {
|
---|
366 | for (var e = 0; e < ExampleCount; e++) {
|
---|
367 | var list = new Person[InstanceCount];
|
---|
368 | for (var i = 0; i < InstanceCount; i++) {
|
---|
369 | list[i] = new Person {
|
---|
370 | Age = pc + e + i,
|
---|
371 | Name = string.Format("{0} - {1} - {2}", pc, e, i)
|
---|
372 | };
|
---|
373 | }
|
---|
374 |
|
---|
375 | Interlocked.Add(ref count, list.Length);
|
---|
376 | }
|
---|
377 | });
|
---|
378 | sw.Stop();
|
---|
379 |
|
---|
380 | Console.WriteLine("3 - Duration: {0} - {1}", sw.ElapsedTicks, sw.Elapsed.TotalSeconds);
|
---|
381 | Console.WriteLine("PartitionSize: {0}", count);
|
---|
382 | }
|
---|
383 |
|
---|
384 | private static void Test2() {
|
---|
385 | var pool = new ObjectPool<Person>(() => new Person(), 2048);
|
---|
386 |
|
---|
387 | Parallel.For(0, 10000, i => {
|
---|
388 | for (var j = 0; j < 3000; j++) {
|
---|
389 | var person = pool.Allocate();
|
---|
390 | person.Age = i + j;
|
---|
391 | person.Name = string.Format("{0} - {1}", i, j);
|
---|
392 | }
|
---|
393 |
|
---|
394 | //pool.Free()
|
---|
395 | });
|
---|
396 | }
|
---|
397 |
|
---|
398 | static void SimpleTest() {
|
---|
399 | var interpreter = new PushInterpreter();
|
---|
400 |
|
---|
401 | interpreter.Run("5 INTEGER.DUP INTEGER.+");
|
---|
402 |
|
---|
403 | interpreter.PrintStacks();
|
---|
404 | }
|
---|
405 |
|
---|
406 | static async Task Stepwise() {
|
---|
407 | var interpreter = new PushInterpreter();
|
---|
408 |
|
---|
409 | interpreter.Run("( 0 2 CODE.QUOTE ( 1 INTEGER.+ 0 3 CODE.QUOTE ( 1 INTEGER.+ INTEGER.* ) CODE.DO*RANGE INTEGER.+ ) CODE.DO*RANGE )");
|
---|
410 |
|
---|
411 | while (!interpreter.IsCompleted) {
|
---|
412 | Console.Clear();
|
---|
413 | interpreter.PrintStacks();
|
---|
414 | interpreter.Step();
|
---|
415 |
|
---|
416 | var input = Console.ReadKey();
|
---|
417 | if (input.Key == ConsoleKey.Escape) {
|
---|
418 | break;
|
---|
419 | } else if (input.Key == ConsoleKey.Spacebar) {
|
---|
420 | await interpreter.ResumeAsync();
|
---|
421 | }
|
---|
422 | }
|
---|
423 |
|
---|
424 | Console.Clear();
|
---|
425 | interpreter.PrintStacks();
|
---|
426 | }
|
---|
427 |
|
---|
428 | static void PerformanceTestInterpreter() {
|
---|
429 | var program = PushParser.Parse("( 5 INTEGER.DUP INTEGER.+ )");
|
---|
430 |
|
---|
431 | var interpreter = new PushInterpreter();
|
---|
432 | var sw = new Stopwatch();
|
---|
433 |
|
---|
434 | sw.Start();
|
---|
435 | for (var i = 0; i < 60000000; i++) {
|
---|
436 | interpreter.Run(program);
|
---|
437 | interpreter.ClearStacks();
|
---|
438 | }
|
---|
439 | sw.Stop();
|
---|
440 |
|
---|
441 | Console.WriteLine(sw.Elapsed);
|
---|
442 | }
|
---|
443 |
|
---|
444 | static void PerformanceParallelTestInterpreter() {
|
---|
445 | var program = PushParser.Parse("( 5 INTEGER.DUP INTEGER.+ )");
|
---|
446 |
|
---|
447 | var sw = new Stopwatch();
|
---|
448 | var iterations = 100;
|
---|
449 | var amount = 600000;
|
---|
450 | var pool = new PushInterpreterPool(iterations, 1024);
|
---|
451 |
|
---|
452 | sw.Start();
|
---|
453 | Parallel.For(0, iterations, i => {
|
---|
454 | using (var interpreter = pool.Create()) {
|
---|
455 | for (var j = 0; j < amount; j++) {
|
---|
456 | interpreter.Run(program);
|
---|
457 | interpreter.ClearStacks();
|
---|
458 | }
|
---|
459 | }
|
---|
460 | });
|
---|
461 | sw.Stop();
|
---|
462 |
|
---|
463 | Console.WriteLine(sw.Elapsed);
|
---|
464 | }
|
---|
465 |
|
---|
466 | static void PerformanceTestRecursiveCodeGenerator() {
|
---|
467 | var sw = new Stopwatch();
|
---|
468 | var random = new FastRandom(1337);
|
---|
469 |
|
---|
470 | sw.Start();
|
---|
471 | var expressions = RecursiveCodeGenerator.RandomCode(60000000, random).ToList();
|
---|
472 | sw.Stop();
|
---|
473 |
|
---|
474 | Console.WriteLine("Generated {0} in {1}", expressions.Count, sw.Elapsed);
|
---|
475 | }
|
---|
476 |
|
---|
477 | static void PerformanceTestLinearCodeGenerator() {
|
---|
478 | var sw = new Stopwatch();
|
---|
479 | var random = new FastRandom(1337);
|
---|
480 | var config = new PushConfiguration();
|
---|
481 |
|
---|
482 | sw.Start();
|
---|
483 | var expressions = LinearCodeGenerator.RandomCode(60000000, null, random, config).ToList();
|
---|
484 | sw.Stop();
|
---|
485 |
|
---|
486 | Console.WriteLine("Generated {0} in {1}", expressions.Count, sw.Elapsed);
|
---|
487 | }
|
---|
488 |
|
---|
489 | static void TestRobustness() {
|
---|
490 | var sw = new Stopwatch();
|
---|
491 | var parallelism = Environment.ProcessorCount;
|
---|
492 | var maxProgramSizeLimit = 1024;
|
---|
493 | var partitionSize = 100000;
|
---|
494 | var execCounters = new long[parallelism];
|
---|
495 |
|
---|
496 | var config = new PushConfiguration {
|
---|
497 | EvalPushLimit = 1024,
|
---|
498 | MaxPointsInProgram = 1024
|
---|
499 | };
|
---|
500 |
|
---|
501 | config.DisableStack(StackTypes.Exec);
|
---|
502 | config.DisableStack(StackTypes.Code);
|
---|
503 | config.DisableStack(StackTypes.Float);
|
---|
504 |
|
---|
505 | var errors = config.EnabledExpressions.Where(
|
---|
506 | name => {
|
---|
507 | var type = ExpressionTable.NameToTypeTable[name];
|
---|
508 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
509 |
|
---|
510 | return attribute.StackType == StackTypes.Exec
|
---|
511 | || attribute.AdditionalStackDependencies.HasFlag(StackTypes.Exec)
|
---|
512 | || attribute.StackType == StackTypes.Code
|
---|
513 | || attribute.AdditionalStackDependencies.HasFlag(StackTypes.Code)
|
---|
514 | || attribute.StackType == StackTypes.Float
|
---|
515 | || attribute.AdditionalStackDependencies.HasFlag(StackTypes.Float);
|
---|
516 | });
|
---|
517 |
|
---|
518 | var pool = new PushInterpreterPool(config);
|
---|
519 |
|
---|
520 | sw.Start();
|
---|
521 | Parallel.For(0, parallelism, i => {
|
---|
522 | var random = new FastRandom(1337);
|
---|
523 |
|
---|
524 | using (var interpreter = pool.Create(random)) {
|
---|
525 | for (var j = 0; j < partitionSize; j++) {
|
---|
526 | var program = LinearCodeGenerator.RandomProgram(maxProgramSizeLimit, random, config);
|
---|
527 | interpreter.Run(program);
|
---|
528 |
|
---|
529 | execCounters[i] += interpreter.ExecCounter;
|
---|
530 | interpreter.Reset();
|
---|
531 | }
|
---|
532 | }
|
---|
533 | });
|
---|
534 | sw.Stop();
|
---|
535 |
|
---|
536 | Console.WriteLine("ExecCount: {0}", execCounters.Sum());
|
---|
537 | Console.WriteLine("Duration: {0}", sw.Elapsed);
|
---|
538 | }
|
---|
539 |
|
---|
540 | static void TestPool() {
|
---|
541 | var pool = new PushInterpreterPool();
|
---|
542 | var normal = new PushInterpreter();
|
---|
543 | int id;
|
---|
544 |
|
---|
545 | using (var interpreter = pool.Create()) {
|
---|
546 | interpreter.Run("( 1 2 INTEGER.+ )");
|
---|
547 |
|
---|
548 | id = interpreter.GetHashCode();
|
---|
549 | if (id == normal.GetHashCode()) Console.WriteLine("equal 1");
|
---|
550 | }
|
---|
551 |
|
---|
552 | using (var interpreter = pool.Create())
|
---|
553 | using (var interpreter2 = pool.Create()) {
|
---|
554 | interpreter.Run("( 1 2 INTEGER.+ )");
|
---|
555 | if (id == interpreter.GetHashCode()) Console.WriteLine("equal 2");
|
---|
556 | if (id == interpreter2.GetHashCode()) Console.WriteLine("equal 3");
|
---|
557 | }
|
---|
558 | }
|
---|
559 |
|
---|
560 | static void TestPoolPerformance() {
|
---|
561 | var sw = new Stopwatch();
|
---|
562 | var iterations = 100000000;
|
---|
563 |
|
---|
564 | sw.Start();
|
---|
565 | for (var i = 0; i < iterations; i++) {
|
---|
566 | var interpreter = new PushInterpreter();
|
---|
567 | interpreter.ClearStacks();
|
---|
568 | }
|
---|
569 | sw.Stop();
|
---|
570 |
|
---|
571 | Console.WriteLine(sw.Elapsed);
|
---|
572 |
|
---|
573 | var pool = new PushInterpreterPool();
|
---|
574 | sw.Restart();
|
---|
575 | for (var i = 0; i < iterations; i++) {
|
---|
576 | using (var interpreter = pool.Create()) {
|
---|
577 | interpreter.ClearStacks();
|
---|
578 | }
|
---|
579 | }
|
---|
580 | sw.Stop();
|
---|
581 |
|
---|
582 | Console.WriteLine(sw.Elapsed);
|
---|
583 | }
|
---|
584 |
|
---|
585 | static void TestSimplifier() {
|
---|
586 | var program = PushParser.ParseProgram("( 5 ( INTEGER.DUP FLOAT.+ FLOAT.- ) ( EXEC.DO ( EXEC.IF ) EXEC.Y ) INTEGER.+ )");
|
---|
587 | var pool = new PushInterpreterPool();
|
---|
588 | var random = new FastRandom(1337);
|
---|
589 |
|
---|
590 | var randomProg = LinearCodeGenerator.RandomProgram(4096 * 4, random); // 53
|
---|
591 |
|
---|
592 | Func<PushProgram, double> evaluator = prog => {
|
---|
593 | using (var interpreter = pool.Create(random)) {
|
---|
594 | interpreter.Run(prog);
|
---|
595 | return interpreter.IntegerStack.IsEmpty
|
---|
596 | ? double.MaxValue
|
---|
597 | : Math.Abs(interpreter.IntegerStack.Top - 10);
|
---|
598 | }
|
---|
599 | };
|
---|
600 |
|
---|
601 | var simplerProgram = Simplifier.Simplify(program, pool.PushConfiguration, evaluator);
|
---|
602 |
|
---|
603 | Console.WriteLine(simplerProgram);
|
---|
604 | }
|
---|
605 | }
|
---|
606 |
|
---|
607 | [Serializable]
|
---|
608 | class Person : IPooledObject {
|
---|
609 | public string Name { get; set; }
|
---|
610 | public int Age { get; set; }
|
---|
611 |
|
---|
612 | public void Init() { }
|
---|
613 |
|
---|
614 | public void Reset() {
|
---|
615 | Age = default(int);
|
---|
616 | Name = null;
|
---|
617 | }
|
---|
618 | }
|
---|
619 | }
|
---|