HexDecodeBenches.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Globalization;
  3. using BenchmarkDotNet.Attributes;
  4. using BenchmarkDotNet.Running;
  5. using MediaBrowser.Common;
  6. namespace Jellyfin.Common.Benches
  7. {
  8. [MemoryDiagnoser]
  9. public class HexDecodeBenches
  10. {
  11. [Params(/*0,*/ 10, 100, 1000, 10000, 1000000)]
  12. public int N { get; set; }
  13. private string data;
  14. [GlobalSetup]
  15. public void GlobalSetup()
  16. {
  17. var tmp = new byte[N];
  18. new Random(42).NextBytes(tmp);
  19. data = Hex.Encode(tmp);
  20. }
  21. public static byte[] DecodeSubString(string str)
  22. {
  23. byte[] bytes = new byte[str.Length / 2];
  24. for (int i = 0; i < str.Length; i += 2)
  25. {
  26. bytes[i / 2] = byte.Parse(
  27. str.Substring(i, 2),
  28. NumberStyles.HexNumber,
  29. CultureInfo.InvariantCulture);
  30. }
  31. return bytes;
  32. }
  33. [Benchmark]
  34. public byte[] Decode() => Hex.Decode(data);
  35. [Benchmark]
  36. public byte[] Decode2() => Hex.Decode2(data);
  37. //[Benchmark]
  38. public byte[] DecodeSubString() => DecodeSubString(data);
  39. }
  40. }