Does constant improve performances?

  • Having "stacked" variables defined in a method body as follows:

    public void foo(){
    int a = 10;
    int b = 90;

    which are used as multipliers for some calculation, and therefore are never changed, does it speed up declaring them constant or not?

  • It might, but needs not to (especially when using a decent compiler) and is most probably negligible compared to the cost of the multiplication.

    If you are on C++11 you might take a look if your code qualifies for constexpr, which will improve performance as it allows the compiler to evaluate (parts of) your expression at compile time.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.