Last active
August 18, 2016 13:20
-
-
Save mattwarren/af0319dc908449239cd3d135e76de4a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Config(typeof(Config))] | |
public class LookingUpValueSerializersByType | |
{ | |
static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run<LookingUpValueSerializersByType>(); | |
} | |
private class Config : ManualConfig | |
{ | |
public Config() | |
{ | |
Add(Job.Clr.WithLaunchCount(1).WithWarmupCount(5).WithTargetCount(5)); | |
Add(JitOptimizationsValidator.FailOnError); | |
Add(RPlotExporter.Default); | |
} | |
} | |
// Choose values that are at the beginning, middle and end of the chain of "if" statements | |
[Params(typeof(string), typeof(bool), typeof(byte[]))] | |
public Type TypeToLookup { get; set; } | |
[Benchmark] | |
public ValueSerializer DictionaryLookup() | |
{ | |
return GetSerializerByTypeLookup(TypeToLookup); | |
} | |
private Serializer serialiser = new Serializer(); | |
[Benchmark] | |
public ValueSerializer IfStatements() | |
{ | |
return GetSerializerByTypeOriginal(TypeToLookup); | |
} | |
[Benchmark] | |
public ValueSerializer IfStatementsOptimised() | |
{ | |
return serialiser.GetSerializerByType(TypeToLookup); | |
} | |
private Dictionary<Type, ValueSerializer> lookup = new Dictionary<Type, ValueSerializer> | |
{ | |
{ typeof(string), StringSerializer.Instance }, | |
{ typeof(int), Int32Serializer.Instance }, | |
{ typeof(long), Int64Serializer.Instance }, | |
{ typeof(short), Int16Serializer.Instance }, | |
{ typeof(uint), UInt32Serializer.Instance }, | |
{ typeof(ulong), UInt64Serializer.Instance }, | |
{ typeof(ushort), UInt16Serializer.Instance }, | |
{ typeof(byte), ByteSerializer.Instance }, | |
{ typeof(sbyte), SByteSerializer.Instance }, | |
{ typeof(bool), BoolSerializer.Instance }, | |
{ typeof(DateTime), DateTimeSerializer.Instance }, | |
{ typeof(Guid), GuidSerializer.Instance }, | |
{ typeof(float), FloatSerializer.Instance }, | |
{ typeof(double), DoubleSerializer.Instance }, | |
{ typeof(decimal), DecimalSerializer.Instance }, | |
{ typeof(char), CharSerializer.Instance }, | |
{ typeof(byte[]), ByteArraySerializer.Instance }, | |
{ typeof(Type), TypeSerializer.Instance }, | |
{ Type.GetType("System.RuntimeType"), TypeSerializer.Instance } | |
}; | |
public static readonly Assembly CoreAssembly = typeof(int).GetTypeInfo().Assembly; | |
public ValueSerializer GetSerializerByTypeLookup(Type type) | |
{ | |
// Do this check to match the behaviour of GetSerializerByType([NotNull] Type type) in Wire.Serialiser | |
if (ReferenceEquals(type.GetTypeInfo().Assembly, CoreAssembly) == false) | |
{ | |
//Fallback | |
return null; | |
} | |
ValueSerializer serializer; | |
if (lookup.TryGetValue(type, out serializer)) | |
return serializer; | |
// Fallback | |
return null; | |
} | |
public ValueSerializer GetSerializerByTypeOriginal(Type type) | |
{ | |
// Do this check to match the behaviour of GetSerializerByType([NotNull] Type type) in Wire.Serialiser | |
if (ReferenceEquals(type.GetTypeInfo().Assembly, CoreAssembly) == false) | |
{ | |
//Fallback | |
return null; | |
} | |
if (type == typeof(string)) | |
return StringSerializer.Instance; | |
if (type == typeof(int)) | |
return Int32Serializer.Instance; | |
if (type == typeof(long)) | |
return Int64Serializer.Instance; | |
if (type == typeof(short)) | |
return Int16Serializer.Instance; | |
if (type == typeof(uint)) | |
return UInt32Serializer.Instance; | |
if (type == typeof(ulong)) | |
return UInt64Serializer.Instance; | |
if (type == typeof(ushort)) | |
return UInt16Serializer.Instance; | |
if (type == typeof(byte)) | |
return ByteSerializer.Instance; | |
if (type == typeof(sbyte)) | |
return SByteSerializer.Instance; | |
if (type == typeof(bool)) | |
return BoolSerializer.Instance; | |
if (type == typeof(DateTime)) | |
return DateTimeSerializer.Instance; | |
if (type == typeof(Guid)) | |
return GuidSerializer.Instance; | |
if (type == typeof(float)) | |
return FloatSerializer.Instance; | |
if (type == typeof(double)) | |
return DoubleSerializer.Instance; | |
if (type == typeof(decimal)) | |
return DecimalSerializer.Instance; | |
if (type == typeof(char)) | |
return CharSerializer.Instance; | |
if (type == typeof(byte[])) | |
return ByteArraySerializer.Instance; | |
if (type == typeof(Type)) | |
return TypeSerializer.Instance; | |
if (type == Type.GetType("System.RuntimeType")) | |
return TypeSerializer.Instance; | |
// Fallback | |
return null; | |
} | |
} |
Host Process Environment Information:
BenchmarkDotNet.Core=v0.9.9.0
OS=Microsoft Windows NT 6.1.7601 Service Pack 1
Processor=Intel(R) Core(TM) i7-4800MQ CPU 2.70GHz, ProcessorCount=8
Frequency=2630761 ticks, Resolution=380.1181 ns, Timer=TSC
CLR=MS.NET 4.0.30319.42000, Arch=32-bit RELEASE
GC=Concurrent Workstation
JitModules=clrjit-v4.6.1076.0
Type=LookingUpValueSerializersByType Mode=Throughput Toolchain=Clr
Runtime=Clr LaunchCount=1 WarmupCount=5
TargetCount=5
Method | TypeToLookup | Median | StdDev |
---|---|---|---|
DictionaryLookup | System.Boolean | 60.7535 ns | 3.1751 ns |
IfStatements | System.Boolean | 50.3454 ns | 2.8762 ns |
IfStatementsOptimised | System.Boolean | 64.0759 ns | 6.6005 ns |
DictionaryLookup | System.Byte[] | 68.0078 ns | 2.8417 ns |
IfStatements | System.Byte[] | 80.9819 ns | 2.3168 ns |
IfStatementsOptimised | System.Byte[] | 83.5075 ns | 2.5113 ns |
DictionaryLookup | System.String | 63.4701 ns | 3.1091 ns |
IfStatements | System.String | 31.9564 ns | 1.3422 ns |
IfStatementsOptimised | System.String | 33.9313 ns | 1.6139 ns |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results