Skip to content

Instantly share code, notes, and snippets.

@EgorBo
Created September 9, 2021 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EgorBo/6a90bfc2eaa37854c4d529fe3eb4afa8 to your computer and use it in GitHub Desktop.
Save EgorBo/6a90bfc2eaa37854c4d529fe3eb4afa8 to your computer and use it in GitHub Desktop.
trie.cs
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
public class Program
{
public static void Main()
{
BenchmarkDotNet.Running.BenchmarkRunner.Run<Program>();
}
public static IEnumerable<string> TestData()
{
yield return "system.void";
yield return "system.v3id";
yield return "system.char";
yield return "system.Char";
yield return "system.byte";
yield return "system.bytt";
yield return "system.int16";
yield return "system.int32";
yield return "system.int64";
yield return "system.sbyte";
yield return "system.uint16";
yield return "system.uint32";
yield return "system.uint64";
yield return "system.single";
yield return "system.double";
yield return "system.string";
yield return "system.object";
yield return "system.decimal";
yield return "system.boolean";
yield return "system.boolean";
yield return "system.booleaen";
}
[ArgumentsSource(nameof(TestData))]
[Benchmark(Baseline = true)]
public string? GetShortName_opt(string str)
{
switch (str.Length - 11)
{
case 0:
switch (str[7])
{
case 'v': return (str == "system.void" ? "void" : null);
case 'c': return (str == "system.char" ? "char" : null);
case 'b': return (str == "system.byte" ? "byte" : null);
default: return null;
}
case 1:
switch (str[10])
{
case '1': return (str == "system.int16" ? "short" : null);
case '3': return (str == "system.int32" ? "int" : null);
case '6': return (str == "system.int64" ? "long" : null);
case 't': return (str == "system.sbyte" ? "sbyte" : null);
default: return null;
}
case 2:
switch (str[8])
{
case 'i': return (str == "system.single" ? "float" : null);
case 'o': return (str == "system.double" ? "double" : null);
case 't': return (str == "system.string" ? "string" : null);
case 'b': return (str == "system.object" ? "object" : null);
default: return null;
}
case 3:
return str == "system.boolean" ? "bool" : null;
}
return null;
}
[ArgumentsSource(nameof(TestData))]
[Benchmark]
public string? GetShortName(string str)
{
switch (str)
{
case "system.int16":
return "short";
case "system.int32":
return "int";
case "system.int64":
return "long";
case "system.string":
return "string";
case "system.object":
return "object";
case "system.boolean":
return "bool";
case "system.void":
return "void";
case "system.char":
return "char";
case "system.byte":
return "byte";
case "system.sbyte":
return "sbyte";
case "system.single":
return "float";
case "system.double":
return "double";
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment