Best practices for performance

Strength reduction

Strength reduction is a program optimization technique that replaces more expensive operations with cheaper ones.

A few examples of strength reduction:

Expensive operationCheaper operation
Division by power of 2a / 2na >> n
Modulo by power of 2a % 2na & (2n – 1)
Replacement of pow() with combination of multiplications, divisions and square rootspow(a, 0.5)pow(a,1.5)pow(a, -1.5)sqrt(a)a * sqrt(a)1.0 / (a * sqrt(a))