In this analysis, we delve into the performance of different methods for creating objects in .NET, utilizing the benchmarking tool BenchmarkDotNet. Our goal is to understand how each method impacts performance and when to use each technique.
- Runtime: .NET 8.0.3 (8.0.324.11423), X64 RyuJIT AVX2
- Garbage Collector: Concurrent Workstation
- System: Windows 11, AMD Ryzen 9 5900X, 1 CPU, 24 logical and 12 physical cores
- .NET SDK: 8.0.202
We compared the following object creation methods:
- Using the
new
Keyword Activator.CreateInstance
with TypeOfActivator.CreateInstance
GenericRuntimeHelpers.GetUninitializedObject
Here's a brief overview of the benchmark results for each method:
- Mean: 2.754 ns
- StdDev: 0.0777 ns
- Memory Allocated: 24 B
This method is the fastest and most straightforward for object instantiation.
- Mean: 7.935 ns
- StdDev: 1.4022 ns
- Memory Allocated: 24 B
Using Activator.CreateInstance
with the type of the object is significantly slower than the new
keyword due to reflection overhead.
- Mean: 7.935 ns
- StdDev: 0.2119 ns
- Memory Allocated: 24 B
Similar to its non-generic counterpart, this method also suffers from the overhead associated with reflection.
- Mean: 24.874 ns
- StdDev: 0.3682 ns
- Memory Allocated: 24 B
Creating an uninitialized object is the slowest method, primarily used in special cases like serialization/deserialization.
- The
new
Keyword: Best for general use due to its speed and simplicity. Activator.CreateInstance
: Useful for scenarios where the type isn't known until runtime but comes with a performance cost.RuntimeHelpers.GetUninitializedObject
: Recommended only for advanced scenarios requiring uninitialized object creation, such as custom serialization mechanisms.
Choosing the right object creation method depends on your specific needs and performance requirements. While the new
keyword is suitable for most scenarios, other methods have their place in more dynamic or specialized contexts.
Method | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio |
---|---|---|---|---|---|---|---|---|---|
NewKeyword | 2.754 ns | 0.0831 ns | 0.0777 ns | 2.773 ns | 1.00 | 0.00 | 0.0014 | 24 B | 1.00 |
ActivatorCreateInstanceWithTypeOf | 7.935 ns | 0.4756 ns | 1.4022 ns | 8.471 ns | 3.02 | 0.53 | 0.0014 | 24 B | 1.00 |
ActivatorCreateInstanceGeneric | 7.935 ns | 0.1980 ns | 0.2119 ns | 7.901 ns | 2.88 | 0.12 | 0.0014 | 24 B | 1.00 |
GetUninitializedObject | 24.874 ns | 0.4153 ns | 0.3682 ns | 24.842 ns | 9.02 | 0.33 | 0.0014 | 24 B | 1.00 |