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