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