You will need to analyze why you have a slowdown with variables, perhaps best of all by looking at the collector, which is created, usually with the -S option to the compiler.
There can be many reasons to speed things up if you have constants:
- Small integer constants can go to build operations
- The compiler can perform a loop reversal
- there may be special arithmetic tricks if the constant is a power of 2
On the other hand, you can slow down if you only pass a pointer to const in your function, and the compiler cannot rule out that your const qualified object is smoothed. const only says that you have no right to change the value, but the compiler cannot know if it changes unexpectedly. Declaring a pointer with restrict can help here.
So, identify the problem points, compare them in assembler with two different versions (constants and const qualified variables) and try to find the reason for the slowdown.
Use inline to troubleshoot in-place optimized issues.
If nothing else helps, and if you manage to localize it, you might even consider writing a script by creating this function with literal constants and compiling this small piece of code before each run. If your runs are long, it may well pay off for a short compilation and reuse.
source share