Created
February 24, 2020 03:31
-
-
Save vanbukin/a5f6f3d294e7b11b8782d79706f9ef75 to your computer and use it in GitHub Desktop.
GuidBenchmarks - MemoryMarshal remove
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
namespace GuidBenchmarks | |
{ | |
public static class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); | |
} | |
} | |
[MemoryDiagnoser] | |
public class GuidBenchmarks | |
{ | |
[GlobalSetup] | |
public void PrintInfo() => Console.WriteLine($"System.Object is defined in {typeof(object).Assembly.Location}"); | |
public static IEnumerable<Guid> GetGuid() | |
{ | |
yield return new Guid(new byte[] {196, 173, 35, 66, 17, 29, 1, 33, 245, 155, 141, 19, 7, 13, 37, 42}); | |
} | |
[Benchmark(OperationsPerInvoke = 1_048_576)] | |
[Arguments(new byte[] {196, 173, 35, 66, 17, 29, 1, 33, 245, 155, 141, 19, 7, 13, 37, 42})] | |
public void CtorReadOnlySpan(ReadOnlySpan<byte> bytes) | |
{ | |
for (int i = 0; i < 1_048_576; i++) | |
{ | |
_ = new Guid(bytes); | |
} | |
} | |
[Benchmark(OperationsPerInvoke = 1_048_576)] | |
[ArgumentsSource(nameof(GetGuid))] | |
public void ToByteArray(Guid guid) | |
{ | |
for (int i = 0; i < 1_048_576; i++) | |
{ | |
_ = guid.ToByteArray(); | |
} | |
} | |
[Benchmark(OperationsPerInvoke = 1_048_576)] | |
[ArgumentsSource(nameof(GetGuid))] | |
public void TryWriteBytes(Guid guid) | |
{ | |
Span<byte> result = stackalloc byte[16]; | |
for (int i = 0; i < 1_048_576; i++) | |
{ | |
_ = guid.TryWriteBytes(result); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment