-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSimpleArrayPoolBenchmarks.cs
More file actions
88 lines (82 loc) · 2.32 KB
/
SimpleArrayPoolBenchmarks.cs
File metadata and controls
88 lines (82 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Buffers;
using BenchmarkDotNet.Attributes;
using Platform.Collections.Arrays;
namespace Platform.Collections.Benchmarks
{
[SimpleJob]
[MemoryDiagnoser]
public class SimpleArrayPoolBenchmarks
{
[Params(64, 256, 1024)]
public int ArraySize { get; set; }
private const int OperationCount = 1000;
[Benchmark(Baseline = true)]
public void StandardArrayAllocation()
{
for (int i = 0; i < OperationCount; i++)
{
var array = new int[ArraySize];
// Simulate some work
array[0] = i;
array[ArraySize - 1] = i;
}
}
[Benchmark]
public void PlatformArrayPool()
{
for (int i = 0; i < OperationCount; i++)
{
var array = ArrayPool.Allocate<int>(ArraySize);
try
{
// Simulate some work
array[0] = i;
array[ArraySize - 1] = i;
}
finally
{
ArrayPool.Free(array);
}
}
}
[Benchmark]
public void SystemBuffersArrayPool()
{
var pool = System.Buffers.ArrayPool<int>.Shared;
for (int i = 0; i < OperationCount; i++)
{
var array = pool.Rent(ArraySize);
try
{
// Simulate some work
array[0] = i;
array[ArraySize - 1] = i;
}
finally
{
pool.Return(array);
}
}
}
[Benchmark]
public void PlatformArrayPoolWithInstance()
{
var platformPool = new Platform.Collections.Arrays.ArrayPool<int>();
for (int i = 0; i < OperationCount; i++)
{
var array = platformPool.Allocate(ArraySize);
try
{
// Simulate some work
array[0] = i;
array[ArraySize - 1] = i;
}
finally
{
platformPool.Free(array);
}
}
}
}
}