C# and .NET • Modern C# baseline
Profiling, Allocations, and Performance
Measure the real workload before optimizing and preserve correctness while improving it.
Managed code performance depends on algorithms, allocations, garbage collection, I/O, and runtime behavior. Profile a representative workload before changing code. Benchmark isolated operations with a proper harness, warm-up, and realistic inputs; avoid treating a single wall-clock measurement as proof.
Key points
- Choose algorithms with suitable complexity before micro-optimizing syntax.
- Allocation reductions matter most when they remove measured pressure.
Example
// Example build:
// dotnet publish -c Release
// Profile the real application with a platform-appropriate .NET profiler.
What to expect
Publish produces an optimized build configuration; profiling steps depend on the target environment.
Practice exercise
Measure a LINQ query and a loop over realistic input, then confirm both return equivalent results.
Key idea: Optimization should be evidence-based and protected by correctness tests.