This recommendation is part of the open catalog of best practice rules for performance that is automatically detected and reported by Codee.
Issue
The pow
mathematical function is computationally expensive and in many cases can be replaced by faster mathematical operations.
Relevance
Function pow
is commonly used in scientific computations. In general, it is an expensive function. However, in some cases when the value of exponent is known at compile time, its runtime can be greatly reduced by replacing it with a combination of multiplications, divisions and square roots.
Actions
Replace the pow
invocation by the corresponding calculation involving multiplications, divisions and/or square roots.
Notes
Some compilers under some circumstances (e.g. relaxed IEEE 754 semantics) can do this optimization automatically. However, doing it manually will guarantee best performance across all the compilers.
Code example
The following code invokes pow
to calculate x^1.5:
void example(float x) {
float res = pow(x, 1.5);
}
This can also be accomplished by multiplying x by its square root, which is faster:
void example(float x) {
float res = x * sqrt(x);
}
Related resources
References

Building performance into the code from day one with Codee