using System.Diagnostics;
public static void Main()
ReadOnlySpan<byte> r = stackalloc byte[] { 52, 48, 48, 55, 48 };
var sw = new Stopwatch();
string sresult = string.Empty;
for (int i=0; i<10000000; i++) sresult = Test0(r);
Console.WriteLine($"Test0 took {sw.ElapsedMilliseconds}ms {sresult}");
for (int i=0; i<10000000; i++) result = Test1 (r);
Console.WriteLine($"Test1 took {sw.ElapsedMilliseconds}ms {result}");
for (int i=0; i<10000000; i++) result = Test2(r);
Console.WriteLine($"Test2 took {sw.ElapsedMilliseconds}ms {result}");
for (int i=0; i<10000000; i++) result = Test3(r);
Console.WriteLine($"Test3 took {sw.ElapsedMilliseconds}ms {result}");
for (int i=0; i<10000000; i++) result = Test4(r);
Console.WriteLine($"Test4 took {sw.ElapsedMilliseconds}ms {result}");
for (int i=0; i<10000000; i++) result = Test5(r);
Console.WriteLine($"Test5 took {sw.ElapsedMilliseconds}ms {result}");
for (int i=0; i<10000000; i++) result = Test6(r);
Console.WriteLine($"Test6 took {sw.ElapsedMilliseconds}ms {sresult}");
public static string Test0(ReadOnlySpan<byte> r)
string s = Encoding.ASCII.GetString(r);
public static int Test1(ReadOnlySpan<byte> r)
string s = Encoding.ASCII.GetString(r);
public static int Test2(ReadOnlySpan<byte> data)
if (System.Buffers.Text.Utf8Parser.TryParse(data, out int value, out int bytes))
public static int Test3(ReadOnlySpan<byte> span)
Span<char> chars = stackalloc char[span.Length];
Encoding.ASCII.GetChars(span, chars);
return(int.Parse(chars));
public static int Test4(ReadOnlySpan<byte> span)
Span<char> chars = stackalloc char[span.Length];
Encoding.ASCII.GetChars(span, chars);
public static int Test5(ReadOnlySpan<byte> source)
if ((uint)index >= (uint)source.Length)
answer = answer * 10 + c - '0';
public static int Test6(ReadOnlySpan<byte> span)
foreach (byte digit in span)
if (digit < 48 || digit > 57)
throw new ArgumentException("Invalid digit in span.");
number = number * 10 + (digit - 48);