Created
August 18, 2016 15:44
-
-
Save mattwarren/b48b3e5a720b174e64f16353d8ce9960 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 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(); | |
} | |
} |
Author
mattwarren
commented
Aug 18, 2016
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