Profile-Guided Optimization (PGO) in C#

What is PGO?

Profile-guided optimization (PGO) collects execution data while the application runs and uses it to optimize performance.

  • Faster execution by optimizing frequently used code paths
  • Smaller memory footprint due to better inlining decisions
  • Better CPU utilization for improved performance

Types of PGO in .NET

a) Static PGO (Ahead-of-Time Compilation - AOT)

Uses profiling data collected during testing and applies optimizations before deployment.

b) Dynamic PGO (JIT Optimization at Runtime)

Uses real-time execution data and optimizes code while the application is running.

How to Enable PGO in .NET 9 / C# 13?

a) Enabling Dynamic PGO

DOTNET_TieredPGO=1
DOTNET_TC_QuickJitForLoops=1

Or enable it in C# code.

AppContext.SetSwitch("System.Runtime.TieredPGO", true);

b) Enabling Static PGO in .NET 9

Collect Profile Data.

dotnet pgo collect --application-path MyApp.dll

Optimize using the collected data

dotnet pgo optimize --input profile_data.pgo

Example: PGO in Action

public int Compute(int x)
{
    if (x == 0) return 10;
    if (x == 1) return 20;
    if (x == 2) return 30;
    return 40;
}

With Dynamic PGO, the JIT compiler will optimize the most frequently used branches for better performance.

When Should You Use PGO?

  • Web applications (ASP.NET Core)
  • Cloud-native applications
  • Game engines (Unity with .NET 9)
  • High-performance computing (HPC) applications

Conclusion

PGO in .NET 9 / C# 13 helps applications run faster and more efficiently by analyzing execution patterns in real time.

Up Next
    Ebook Download
    View all
    Learn
    View all