Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 PooledPushProgram reduces memory usage and increases performance

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