mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
Add type switch matching benchmark (#3599)
This commit is contained in:
71
Robust.Benchmarks/TypeMatching/TypeSwitchBenchmark.cs
Normal file
71
Robust.Benchmarks/TypeMatching/TypeSwitchBenchmark.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using Robust.Shared.Analyzers;
|
||||
|
||||
namespace Robust.Benchmarks.TypeMatching;
|
||||
|
||||
[MemoryDiagnoser]
|
||||
[Virtual]
|
||||
public class TypeSwitchBenchmark
|
||||
{
|
||||
private readonly Matcher<Struct> _matcher = new();
|
||||
|
||||
[Benchmark]
|
||||
public int BenchmarkInt()
|
||||
{
|
||||
return _matcher.TypeToInt<int>();
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public int BenchmarkString()
|
||||
{
|
||||
return _matcher.TypeToInt<string>();
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public int BenchmarkStruct()
|
||||
{
|
||||
return _matcher.TypeToInt<Struct>();
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public int BenchmarkDouble()
|
||||
{
|
||||
return _matcher.TypeToInt<double>();
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public int BenchmarkFloat()
|
||||
{
|
||||
return _matcher.TypeToInt<float>();
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public int BenchmarkClass()
|
||||
{
|
||||
return _matcher.TypeToInt<Class>();
|
||||
}
|
||||
|
||||
private class Matcher<T1>
|
||||
{
|
||||
public int TypeToInt<T>(T val = default!)
|
||||
{
|
||||
return val switch
|
||||
{
|
||||
int => 1,
|
||||
string => 2,
|
||||
double => 3,
|
||||
T1 => 4,
|
||||
Class => 5,
|
||||
_ => 6
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private struct Struct
|
||||
{
|
||||
}
|
||||
|
||||
private class Class
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user