more than just writing fast code

About

Company
Vintecc
Location
Belgium
Competences

Coding

C#

code performance in .NET

how small adjustments can lead to big performance gains

Within Vintecc, we are constantly improving our software solutions. We do this not only by embracing new technologies, but also by taking a critical look at how we make best use of existing technology. During a recent Vintecc Academy session, we therefore delved into a hot topic for .NET developers: code performance.

In an industrial context, speed and reliability are often key. Think of real-time data processing, quick decisions at the edge, or the smooth handling of thousands of transactions per second. A small inefficiency in the code can then have major consequences. It is therefore essential to program not only correctly, but also efficiently.

Exceptions: useful, but not always innocent

One of the most striking insights from the session was the effect of exception handling on performance. Exceptions are meant to handle exceptional situations - for example, when a network connection fails. But when used as normal flow control (e.g. when checking a balance), they can cost quite a bit of performance.

Although .NET 9 already brought significant improvements in this, throwing exceptions still has a huge performance impact.

 

Case study: payment processing

Suppose you are developing an application that handles payments. When the user's balance is insufficient, you can:
  1. Throw an exception: throw new InsufficientBalanceException();
  2. Return a Result-object with a clear Success or Failure
In our test, the second approach was up to 82% faster, with less memory usage and a more readable structure. Fewer exceptions therefore not only means better performance, but also more maintenance-friendly code.

When should you optimise?


Optimisation only makes sense if there is a measurable problem. That is why it is important to measure before you make any changes. Tools such as Visual Studio Profiler or even a simple  Stopwatch can provide a lot of insight. Focus on code that is executed frequently or that runs within loops

 
A useful tip from the session: pause your application while it is running and see where it “hangs”. This often immediately reveals the most time-consuming methods.

Specific tips for more efficient programming

  • Do not use exceptions for normal flow control.
  • ChooseSpan<T> where possible – ideal for high-performance data structures.
  • Provide capacity when creating List of Dictionary – avoid repeated allocations.
  • Work with Result-types of pattern matching instead of try-catch.
  • Measure what you change – small changes can have a big impact.
  • Maintain readability – performance optimisation should not be an excuse for incomprehensible code.