-
Notifications
You must be signed in to change notification settings - Fork 272
/
UnmanagedMemoryStreamTests.cs
53 lines (47 loc) · 1.33 KB
/
UnmanagedMemoryStreamTests.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
42
43
44
45
46
47
48
49
50
51
52
53
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace System.IO.Tests
{
[BenchmarkCategory(Categories.Libraries)]
public unsafe class UnmanagedMemoryStreamTests
{
private const int Length = 10000;
private byte* _buffer;
[GlobalSetup]
public void Setup()
{
_buffer = (byte*)Marshal.AllocCoTaskMem(Length);
}
[GlobalCleanup]
public void Cleanup()
{
Marshal.FreeCoTaskMem((IntPtr)_buffer);
}
[Benchmark]
public void ReadAsBytes()
{
using (var ums = new UnmanagedMemoryStream(_buffer, Length))
{
while (ums.ReadByte() >= 0)
{
}
}
}
[Benchmark]
public void ReadAsArrays()
{
var array = new byte[128];
using (var ums = new UnmanagedMemoryStream(_buffer, Length))
{
while (ums.Read(array, 0, array.Length) != 0)
{
}
}
}
}
}