using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains.InProcess.Emit;
using System.Collections.Generic;
using System.Runtime.Serialization;
[assembly: System.Diagnostics.Debuggable(isJITTrackingEnabled: false, isJITOptimizerDisabled: false)]
BenchmarkRunner.Run<Benchmark>(
.WithToolchain(new InProcessEmitToolchain(
timeout: TimeSpan.FromSeconds(9),
.AddLogger(new ConsoleLogger(unicodeSupport: true, ConsoleLogger.CreateGrayScheme()))
.WithOptions(ConfigOptions.DisableLogFile));
public static class SerializationInfoExtensions
public static Object GetValueNoThrow_Exception(this SerializationInfo serializationInfo, string name, Type type)
return serializationInfo.GetValue(name, type);
catch (SerializationException)
private static MethodInfo _GetValueNoThrow = typeof(SerializationInfo).GetMethod("GetValueNoThrow", BindingFlags.Instance | BindingFlags.NonPublic);
public static Object GetValueNoThrow_Reflection(this SerializationInfo info, string name, Type type)
return _GetValueNoThrow.Invoke(info, new object[] { name, type });
_si = new SerializationInfo(typeof(Dictionary<string, string>), new FormatterConverter());
_names = new string[10000];
var random = new Random((int)DateTime.Now.Ticks);
for (var i = 0; i < _names.Length; i++)
_names[i] = random.NextInt64().ToString();
if (random.Next() % 100 < 50)
_si.AddValue(_names[i], true);
public void ExceptionOverhead()
for (var i = 0; i < _names.Length; i++)
_si.GetValueNoThrow_Exception(_names[i], typeof(bool));
public void ReflectionOverhead()
for (var i = 0; i < _names.Length; i++)
_si.GetValueNoThrow_Reflection(_names[i], typeof(bool));