Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/TestPooling/Program.cs @ 14745

Last change on this file since 14745 was 14745, checked in by pkimmesw, 7 years ago

#2665 Merged ExecExpandExpression and PushProgram due to performance reasons, Tested managed object pooling

File size: 3.3 KB
Line 
1using System.Threading.Tasks;
2
3namespace TestPooling {
4  using System;
5  using System.Diagnostics;
6  using System.Threading;
7
8  using TestPooling.Pool;
9
10  class Program {
11    private const int PartitionSize = 512;
12    private const int ExampleCount = 200;
13    private const int InstanceCount = 1024;
14    private const int PopulationCount = 100;
15
16    private static int TotalInstanceCount => PopulationCount * InstanceCount * ExampleCount;
17
18
19    static void Main(string[] args) {
20      Test1();
21      Test1_2();
22      Test3();
23    }
24
25    private static void Test1() {
26      var sw = new Stopwatch();
27
28      sw.Start();
29      Parallel.For(0, PopulationCount, pc => {
30        for (var e = 0; e < ExampleCount; e++) {
31          using (var pool = new ManagedPool<Person>(PartitionSize, () => new Person())) {
32            for (var i = 0; i < InstanceCount; i++) {
33              var person = pool.Get();
34              person.Age = pc + e + i;
35              person.Name = string.Format("{0} - {1} - {2}", pc, e, i);
36            }
37          }
38        }
39      });
40      sw.Stop();
41
42      Console.WriteLine("1 - Duration: {0} - {1} for {2}/{3} instances", sw.ElapsedTicks, sw.Elapsed.TotalSeconds, ManagedPool<Person>.InstanceCount, TotalInstanceCount);
43    }
44
45    private static void Test1_2() {
46      var sw = new Stopwatch();
47      sw.Start();
48
49      var provider = new ManagedPoolProvider<Person>(PartitionSize);
50      provider.InitDummyPartition(() => new Person());
51
52      Parallel.For(0, PopulationCount, pc => {
53        for (var e = 0; e < ExampleCount; e++) {
54          using (var pool = provider.CreatePool()) {
55            for (var i = 0; i < InstanceCount; i++) {
56              var person = pool.Get();
57              person.Age = pc + e + i;
58              person.Name = string.Format("{0} - {1} - {2}", pc, e, i);
59            }
60          }
61        }
62      });
63      sw.Stop();
64
65      Console.WriteLine("1_2 - Duration: {0} - {1} for {2}/{3} instances", sw.ElapsedTicks, sw.Elapsed.TotalSeconds, provider.InstanceCount, TotalInstanceCount);
66    }
67
68    private static void Test3() {
69      var count = 0;
70      var sw = new Stopwatch();
71
72      sw.Start();
73      Parallel.For(0, PopulationCount, pc => {
74        for (var e = 0; e < ExampleCount; e++) {
75          var list = new Person[InstanceCount];
76          for (var i = 0; i < InstanceCount; i++) {
77            list[i] = new Person {
78              Age = pc + e + i,
79              Name = string.Format("{0} - {1} - {2}", pc, e, i)
80            };
81          }
82
83          Interlocked.Add(ref count, list.Length);
84        }
85      });
86      sw.Stop();
87
88      Console.WriteLine("3 - Duration: {0} - {1}", sw.ElapsedTicks, sw.Elapsed.TotalSeconds);
89      Console.WriteLine("PartitionSize: {0}", count);
90    }
91
92    private static void Test2() {
93      var pool = new ObjectPool<Person>(() => new Person(), 2048);
94
95      Parallel.For(0, 10000, i => {
96        for (var j = 0; j < 3000; j++) {
97          var person = pool.Allocate();
98          person.Age = i + j;
99          person.Name = string.Format("{0} - {1}", i, j);
100        }
101
102        //pool.Free()
103      });
104    }
105  }
106
107  [Serializable]
108  class Person {
109    public string Name { get; set; }
110    public int Age { get; set; }
111  }
112}
Note: See TracBrowser for help on using the repository browser.