aboutsummaryrefslogtreecommitdiff
path: root/benches/Jellyfin.Common.Benches/HexDecodeBenches.cs
diff options
context:
space:
mode:
Diffstat (limited to 'benches/Jellyfin.Common.Benches/HexDecodeBenches.cs')
-rw-r--r--benches/Jellyfin.Common.Benches/HexDecodeBenches.cs45
1 files changed, 45 insertions, 0 deletions
diff --git a/benches/Jellyfin.Common.Benches/HexDecodeBenches.cs b/benches/Jellyfin.Common.Benches/HexDecodeBenches.cs
new file mode 100644
index 000000000..d9a107b69
--- /dev/null
+++ b/benches/Jellyfin.Common.Benches/HexDecodeBenches.cs
@@ -0,0 +1,45 @@
+using System;
+using System.Globalization;
+using BenchmarkDotNet.Attributes;
+using BenchmarkDotNet.Running;
+using MediaBrowser.Common;
+
+namespace Jellyfin.Common.Benches
+{
+ [MemoryDiagnoser]
+ public class HexDecodeBenches
+ {
+ private string _data;
+
+ [Params(0, 10, 100, 1000, 10000, 1000000)]
+ public int N { get; set; }
+
+ [GlobalSetup]
+ public void GlobalSetup()
+ {
+ var bytes = new byte[N];
+ new Random(42).NextBytes(bytes);
+ _data = Hex.Encode(bytes);
+ }
+
+ [Benchmark]
+ public byte[] Decode() => Hex.Decode(_data);
+
+ [Benchmark]
+ public byte[] DecodeSubString() => DecodeSubString(_data);
+
+ private static byte[] DecodeSubString(string str)
+ {
+ 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;
+ }
+ }
+}