81 lines
2.1 KiB
Plaintext
81 lines
2.1 KiB
Plaintext
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/sealed[The `sealed` keyword]
|
|
|
|
=== Articles & blog posts
|
|
|
|
* https://code-maze.com/improve-performance-sealed-classes-dotnet[Boosting Performance With Sealed Classes in .NET]
|
|
* https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-6/#peanut-butter[Performance Improvements in .NET 6]
|
|
|
|
=== Benchmarks
|
|
|
|
[options="header"]
|
|
|===
|
|
|Method | Runtime | Mean | StdDev | Ratio
|
|
| UnsealedType | .NET 5.0 | 918.7 us | 10.72 us | 1.00
|
|
| SealedType | .NET 5.0 | 231.2 us | 3.20 us | 0.25
|
|
| UnsealedType | .NET 6.0 | 867.9 us | 5.65 us | 1.00
|
|
| SealedType | .NET 6.0 | 218.4 us | 0.59 us | 0.25
|
|
| UnsealedType | .NET 7.0 | 1,074.5 us | 3.15 us | 1.00
|
|
| SealedType | .NET 7.0 | 216.1 us | 1.19 us | 0.20
|
|
|===
|
|
|
|
The results were generated 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() { }
|
|
}
|
|
----
|
|
|
|
Hardware Configuration:
|
|
|
|
[source]
|
|
----
|
|
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
|
|
----
|