Back to results for “Performance”
C++ › Constexpr & Compile-Time

How does constexpr improve performance?

Intermediate

Answer

By evaluating expressions during compilation, constexpr moves computation out of the runtime path—constants, lookup tables, and derived values are baked into the binary, eliminating startup or per-call cost. It also enables the compiler to optimize based on known values. The runtime simply uses the precomputed results.
constexpr int limit = compute();  // no runtime work to get 'limit'
Real-world example Computing configuration constants at compile time removes their setup cost from application startup.

Common follow-ups: What runtime cost does it remove? | Does it always run at compile time?