jellyfin/benches/Jellyfin.Common.Benches/HexDecodeBenches.cs

46 lines
1.1 KiB
C#
Raw Normal View History

2019-10-18 15:22:08 -07:00
using System;
using System.Globalization;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using MediaBrowser.Common;
namespace Jellyfin.Common.Benches
{
[MemoryDiagnoser]
public class HexDecodeBenches
{
2019-11-01 08:26:54 -07:00
private string _data;
2019-10-18 15:22:08 -07:00
2019-11-01 08:26:54 -07:00
[Params(0, 10, 100, 1000, 10000, 1000000)]
public int N { get; set; }
2019-10-19 15:05:04 -07:00
[GlobalSetup]
public void GlobalSetup()
2019-10-18 15:22:08 -07:00
{
2019-11-01 08:26:54 -07:00
var bytes = new byte[N];
new Random(42).NextBytes(bytes);
_data = Hex.Encode(bytes);
2019-10-18 15:22:08 -07:00
}
2019-11-01 08:26:54 -07:00
[Benchmark]
public byte[] Decode() => Hex.Decode(_data);
[Benchmark]
public byte[] DecodeSubString() => DecodeSubString(_data);
private static byte[] DecodeSubString(string str)
2019-10-18 15:22:08 -07:00
{
byte[] bytes = new byte[str.Length / 2];
for (int i = 0; i < str.Length; i += 2)
{
bytes[i / 2] = byte.Parse(
str.Substring(i, 2),
NumberStyles.HexNumber,
CultureInfo.InvariantCulture);
}
return bytes;
}
}
}