67 lines
1.9 KiB
Plaintext
67 lines
1.9 KiB
Plaintext
![]() |
=== What is the potential impact?
|
||
|
|
||
|
We measured at least 4x improvement in the execution time by running the following snippet with https://github.com/dotnet/BenchmarkDotNet[BenchmarkDotNet].
|
||
|
|
||
|
[source,csharp]
|
||
|
----
|
||
|
[Params(1_000_000)]
|
||
|
public int Iterations { get; set; }
|
||
|
|
||
|
private readonly UnsealedClass unsealedType = new UnsealedClass();
|
||
|
private readonly SealedClass sealedType = new SealedClass();
|
||
|
|
||
|
[Benchmark(Baseline = true)]
|
||
|
public void UnsealedType()
|
||
|
{
|
||
|
for (int i = 0; i < Iterations; i++)
|
||
|
{
|
||
|
unsealedType.DoNothing();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
[Benchmark]
|
||
|
public void SealedType()
|
||
|
{
|
||
|
for (int i = 0; i < Iterations; i++)
|
||
|
{
|
||
|
sealedType.DoNothing();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private class BaseType
|
||
|
{
|
||
|
public virtual void DoNothing() { }
|
||
|
}
|
||
|
|
||
|
private class UnsealedClass : BaseType
|
||
|
{
|
||
|
public override void DoNothing() { }
|
||
|
}
|
||
|
|
||
|
private sealed class SealedClass : BaseType
|
||
|
{
|
||
|
public override void DoNothing() { }
|
||
|
}
|
||
|
----
|
||
|
|
||
|
[options="header"]
|
||
|
|===
|
||
|
|Method | Runtime | Iterations | Mean | Error | StdDev | Ratio
|
||
|
| UnsealedType | .NET 5.0 | 1000000 | 918.7 us | 12.09 us | 10.72 us | 1.00
|
||
|
| SealedType | .NET 5.0 | 1000000 | 231.2 us | 3.61 us | 3.20 us | 0.25
|
||
|
| UnsealedType | .NET 6.0 | 1000000 | 867.9 us | 6.38 us | 5.65 us | 1.00
|
||
|
| SealedType | .NET 6.0 | 1000000 | 218.4 us | 0.71 us | 0.59 us | 0.25
|
||
|
| UnsealedType | .NET 7.0 | 1000000 | 1,074.5 us | 3.55 us | 3.15 us | 1.00
|
||
|
| SealedType | .NET 7.0 | 1000000 | 216.1 us | 1.28 us | 1.19 us | 0.20
|
||
|
|===
|
||
|
|
||
|
Configuration used for running the benchmarks:
|
||
|
```
|
||
|
BenchmarkDotNet=v0.13.5, OS=Windows 10 (10.0.19045.2846/22H2/2022Update)
|
||
|
12th Gen Intel Core i7-12800H, 1 CPU, 20 logical and 14 physical cores
|
||
|
.NET SDK=7.0.203
|
||
|
[Host] : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2
|
||
|
.NET 5.0 : .NET 5.0.17 (5.0.1722.21314), X64 RyuJIT AVX2
|
||
|
.NET 6.0 : .NET 6.0.16 (6.0.1623.17311), X64 RyuJIT AVX2
|
||
|
.NET 7.0 : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2
|
||
|
```
|