Skip to content

Instantly share code, notes, and snippets.

@admir-live
Created May 20, 2024 13:05
Show Gist options
  • Save admir-live/e55961d07f83024fa531c26372e44aba to your computer and use it in GitHub Desktop.
Save admir-live/e55961d07f83024fa531c26372e44aba to your computer and use it in GitHub Desktop.
Understanding Class Performance in C#
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace ClassBenchmark
{
public class StandardClass
{
public int StandardProperty { get; set; }
public int GetStandardProperty() => StandardProperty;
}
public sealed class SealedClass
{
public int SealedProperty { get; set; }
public int GetSealedProperty() => SealedProperty;
}
public abstract class AbstractBaseClass
{
public virtual int GetBaseProperty() => 0;
}
public class DerivedClass : AbstractBaseClass
{
public int DerivedProperty { get; set; }
public override int GetBaseProperty() => DerivedProperty;
}
public class ExtendedStandardClass : StandardClass
{
public int AdditionalProperty { get; set; }
public int GetAdditionalProperty() => AdditionalProperty;
}
public class ClassBenchmark
{
private readonly StandardClass standardClassInstance = new StandardClass { StandardProperty = 42 };
private readonly SealedClass sealedClassInstance = new SealedClass { SealedProperty = 42 };
private readonly DerivedClass derivedClassInstance = new DerivedClass { DerivedProperty = 42 };
private readonly ExtendedStandardClass extendedClassInstance = new ExtendedStandardClass { StandardProperty = 42, AdditionalProperty = 84 };
[Benchmark(Baseline = true)]
public int BenchmarkStandardClassProperty() => standardClassInstance.GetStandardProperty();
[Benchmark]
public int BenchmarkSealedClassProperty() => sealedClassInstance.GetSealedProperty();
[Benchmark]
public int BenchmarkDerivedClassProperty() => derivedClassInstance.GetBaseProperty();
[Benchmark]
public int BenchmarkExtendedClassStandardProperty() => extendedClassInstance.GetStandardProperty();
[Benchmark]
public int BenchmarkExtendedClassAdditionalProperty() => extendedClassInstance.GetAdditionalProperty();
}
class Program
{
static void Main(string[] args)
{
var benchmarkSummary = BenchmarkRunner.Run<ClassBenchmark>();
}
}
}
@admir-live
Copy link
Author

Benchmark Results

Benchmark Results

Method Average Time (ns) Error (ns) StdDev (ns) Median (ns) Ratio
BenchmarkStandardClassProperty 0.0035 0.0079 0.0070 0.0000 1.00
BenchmarkSealedClassProperty 0.0294 0.0237 0.0282 0.0158 8.40
BenchmarkDerivedClassProperty 0.2313 0.0059 0.0050 0.2309 66.09
BenchmarkExtendedClassStandardProperty 0.0048 0.0079 0.0073 0.0000 1.37
BenchmarkExtendedClassAdditionalProperty 0.0104 0.0102 0.0090 0.0102 2.97

Explanation of Results

Standard Class (StandardClass)

  • Average Time: 0.0035 ns
  • This is a basic class without any additional modifiers or inheritance. It is fast because it lacks additional complexity.

Sealed Class (SealedClass)

  • Average Time: 0.0294 ns
  • Sealed classes are slightly slower than standard classes. Although they cannot be inherited, compiler optimizations can impact performance.

Derived Class (DerivedClass)

  • Average Time: 0.2313 ns
  • This class inherits from an abstract base class. Due to virtual methods and polymorphism, derived classes are slower due to additional checks and indirection.

Extended Standard Class (ExtendedStandardClass)

  • Standard Properties: 0.0048 ns
  • Additional Properties: 0.0104 ns
  • This class inherits from StandardClass and adds new properties. Access to standard properties is slightly slower than the base class, and access to additional properties is slower due to extra complexity.

Usage Guidelines

StandardClass

  • Use for: Simple objects without the need for inheritance.
  • Performance: Best performance.

SealedClass

  • Use for: Preventing class inheritance.
  • Advantages: Good for security and design clarity but might be slightly slower.

DerivedClass

  • Use for: When abstraction and polymorphism are needed.
  • Performance: Always slower due to added complexity.

ExtendedStandardClass

  • Use for: Extending functionality of the standard class.
  • Consideration: Be aware of potential performance costs.

Factors Affecting Performance

  1. Virtual Methods: Slowdown due to additional checks.
  2. Inheritance: Introduces additional complexity and overhead.
  3. Compiler Optimizations: Can either improve or degrade performance, depending on class structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment