Skip to content

Instantly share code, notes, and snippets.

@discostu105
Created March 6, 2019 12:00
Show Gist options
  • Save discostu105/4d208e20619295bddf98afdbe8543ae0 to your computer and use it in GitHub Desktop.
Save discostu105/4d208e20619295bddf98afdbe8543ae0 to your computer and use it in GitHub Desktop.
long parsing benchmarks
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Globalization;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<LongParsingBenchmarks>();
}
}
//[ShortRunJob]
public class LongParsingBenchmarks
{
private readonly string str = "1234567890";
private long result;
[Benchmark]
public void Long_Parse()
{
result = long.Parse(str);
}
[Benchmark]
public void Long_TryParse()
{
long.TryParse(str, out result);
}
[Benchmark]
public void Long_TryParse_NumberStyles_None()
{
long.TryParse(str, NumberStyles.None, CultureInfo.InvariantCulture, out result);
}
[Benchmark]
public void Convert_ToInt64()
{
result = Convert.ToInt64(str);
}
[Benchmark]
public void LongParseFast()
{
result = LongParseFast(str);
}
public static long LongParseFast(ReadOnlySpan<char> value)
{
long result = 0;
for (int i = 0; i < value.Length; i++)
{
result = 10 * result + (value[i] - 48);
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment