# Use constexpr to Accelerate C++ Programs

## Introduction

In some scenarios, our program would call some functions func with the exact same argument N several times. Computing the return value of such functions during runtime might take a while if the cost of computing func(N) is high. However, with the constexpr specifier, we could compute the value of func(N) during compile-time and save it to the program. During runtime, whenever the program sees func(N), it would treat it as a constant and get the value directly from the program without conducting the computation.

In this blog post, I am going to demonstrate the constexpr specifier is capable of accelerating programs by computing the value of functions during runtime.

## Example

We prepared two sets of functions, fib1 and fib2 are used to compute Fibonacci number, getSize1 and getSize2 are just identity functions. The difference between fib1 and fib2, getSize1 and getSize2, is the constexpr specifier.

The constexpr specifier would try to compute the value of expressions to compile-time constants if it is possible. In this way, the program runs much faster.

To compile the program, run the following command in the terminal.

Note that -Werror=vla is to disable VLA for g++ for non-standard dynamic array creation.

We found that the fib1(30) has been treated as a compile-time constant. Therefore, the “computation” of the first Fibonacci number took 360 ns. Because fib2 does not have the constexpr specifier, the fib2(30) needed to be computed during runtime. Therefore, it took 5603501 ns, much longer, to compute the exact same Fibonacci number. Although fib1 does have the constexpr specifier, fib1(n), however, has to be computed during runtime, because n is a runtime variable. Therefore, even though we asked the computer to compute the exact same Fibonacci number, it still took 4179985 ns to run.

## Difference to the inline Specifier

The inline specifier could significantly reduce the time of function call overhead. Therefore, if the function is frequently called and fast to compute, we should always consider using the inline specifier. However, the functions are always executed during runtime. constexpr might be better used for functions that take long to compute because the values might be already computed during compile time.

## Conclusions

If you find your program is slow because of repeatedly computing the same constants during runtime. Try to add constexpr to some key functions for those computations.

Use constexpr to Accelerate C++ Programs

https://leimao.github.io/blog/constexpr-Specifier-CPP/

Lei Mao

12-24-2019

12-24-2019