-
Notifications
You must be signed in to change notification settings - Fork 272
/
Perf_GC.cs
36 lines (32 loc) · 1.23 KB
/
Perf_GC.cs
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
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using System.Collections.Generic;
namespace System.Tests
{
[GenericTypeArguments(typeof(byte))]
[GenericTypeArguments(typeof(char))]
[BenchmarkCategory(Categories.Runtime)]
public class Perf_GC<T>
{
[Benchmark]
[Arguments(1000)]
[Arguments(10_000)]
public T[] NewOperator_Array(int length) => new T[length];
#if NET5_0_OR_GREATER // API introduced in .NET 5
public static IEnumerable<object[]> GetArguments()
{
foreach (int length in new [] { 1000, 10_000 }) // both test cases excercise different code paths
foreach (bool pinned in new[] { true, false })
yield return new object[] { length, pinned };
}
[BenchmarkCategory(Categories.NoWASM)]
[Benchmark]
[ArgumentsSource(nameof(GetArguments))]
public T[] AllocateArray(int length, bool pinned) => GC.AllocateArray<T>(length, pinned);
[BenchmarkCategory(Categories.NoWASM)]
[Benchmark]
[ArgumentsSource(nameof(GetArguments))]
public T[] AllocateUninitializedArray(int length, bool pinned) => GC.AllocateUninitializedArray<T>(length, pinned);
#endif
}
}