Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
[Config(typeof(Config))]
public class FastCreationOfEmptyObjects
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<FastCreationOfEmptyObjects>();
}
private class Config : ManualConfig
{
public Config()
{
Add(Job.Clr.WithLaunchCount(1).WithWarmupCount(5).WithTargetCount(5));
Add(JitOptimizationsValidator.FailOnError);
Add(RPlotExporter.Default);
}
}
public class TestClass
{
public TestClass()
{
// Delibrately empty ctor
}
}
static Type typeToUse = typeof(TestClass);
Func<TestClass> optimisation = Expression.Lambda<Func<TestClass>>(ExpressionEx.GetNewExpression(typeToUse)).Compile();
[Benchmark]
public TestClass GetUninitializedObject()
{
return (TestClass)typeToUse.GetEmptyObject();
}
[Benchmark]
public TestClass OptimisedVersion()
{
return optimisation();
}
}
Owner

mattwarren commented Aug 18, 2016

Results:

image

Owner

mattwarren commented Aug 18, 2016

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=FastCreationOfEmptyObjects  Mode=Throughput  Toolchain=Clr  
Runtime=Clr  LaunchCount=1  WarmupCount=5  
TargetCount=5  
Method Median StdDev
GetUninitializedObject 81.4816 ns 2.5651 ns
OptimisedVersion 12.5316 ns 0.3186 ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment