Skip to content

Instantly share code, notes, and snippets.

@mattwarren
Created August 18, 2016 15:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mattwarren/b48b3e5a720b174e64f16353d8ce9960 to your computer and use it in GitHub Desktop.
[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();
}
}
@mattwarren
Copy link
Author

Results:

image

@mattwarren
Copy link
Author

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