generated from dotnet/new-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
41 lines (36 loc) · 1.13 KB
/
Program.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
37
38
39
40
41
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace ByteArrayAllocation
{
class Program
{
static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
[MemoryDiagnoser]
public class Benchmark
{
public IEnumerable<int> Data()
{
yield return 1;
yield return 1024;
yield return 32 * 1024;
yield return 64 * 1024;
yield return 128 * 1024;
yield return 1024 * 1024;
}
[Benchmark(Baseline = true)]
[ArgumentsSource(nameof(Data))]
public byte[] ArrayAllocation(int size) => new byte[size];
[Benchmark()]
[ArgumentsSource(nameof(Data))]
public byte[] GC_AllocateUninitializedArray(int size) => GC.AllocateUninitializedArray<byte>(size);
[Benchmark()]
[ArgumentsSource(nameof(Data))]
public byte[] GC_AllocateArray(int size) => GC.AllocateArray<byte>(size);
}
}