0

I am currently working in a piece of software, which is highly performance critical, so each optimization counts for me. There is one critical situation that very frequently occurs inside a loop in which I calculate two int indices of a std::vector by using double (for clarification, convert metric positions to map-positions).

There are three sensible ways to do that in my opinion.

//first possibility

int indexX, indexY;
for(int x = 0; x <= xMax; ++x)
{
  for(int y = 0; y <= yMax; ++y)
  {
    indexX = //calculate value using x somehow
    indexY = //calculate value using y somehow
    //do multiple things with indexX and indexY 
  }
}

//second possibility

for(int x = 0; x <= xMax; ++x)
{
  for(int y = 0; y <= yMax; ++y)
  {
    int indexX = //calculate value using x somehow
    int indexY = //calculate value using y somehow
    //do multiple things with indexX and indexY 
  }
}

//third possibility

for(int x = 0; x <= xMax; ++x)
{
  for(int y = 0; y <= yMax; ++y)
  {
    const int indexX = //calculate value using x somehow
    const int indexY = //calculate value using y somehow
    //do multiple things with indexX and indexY 
  }
}

After some search on SO, people generally seem to not recommend the first case and say that better optimizations are possible, if variables are declared as locally as possible. I have tested that and it seems to be correct so far, IF optimizations are turned on during compiling.

However, I am not sure about cases 2/3. All topics on const I could find on SO concern using the keyword as a modifier for function parameters, rather than local variables. Sell me on const correctness is a very general discussion on the topic and mostly deals with "accidental error protection". The accepted answer also states compiler optimizations "are possible", but I could not observe any performance differences in my case.

I understand that the compiler will most likely convert something like const int number = 5 to the actual number (as stated here, it's C#, but I don't expect it to differ for C++) however, in my case it is not known during compile time.

Does the compiler detect that a local variable is only assigned to once and is thus guaranteed to treat both cases the same? Could it be that one might lead to better optimizations than the other? Are those optimizations always better for one case than the other or could it switch between the two? Might it depend on the platform?

Edit: I should mention. The code WILL be compiled on "highly different platforms" and I unfortunately, I cannot inspect the assembly outcome in most cases.

Community
  • 1
  • 1
philkark
  • 2,417
  • 7
  • 38
  • 59
  • If it is known at compile-time, prefer `constexpr` over `const` – CinCout Jun 30 '16 at 11:20
  • I would expect the compiler to detect a constant value even if it is not explicitly declared as `const`, ie. no performance difference between 2/3 – 463035818_is_not_an_ai Jun 30 '16 at 11:20
  • 2
    I would expect same generated code for the 3 snippets. And as usual, use profiler to spot bottleneck. – Jarod42 Jun 30 '16 at 11:28
  • @Jarod42 I actually have observed performance difference between case 1 and 2/3 while optimized compilation. – philkark Jun 30 '16 at 11:30
  • @CinCout Thanks for the advice! Unfortunately, it is not know during compile time. – philkark Jun 30 '16 at 11:30
  • Here the const is purely for the programmer (and reader of the code): It's a statement (insurance) that the value does not change. –  Jun 30 '16 at 11:31
  • ^does *not* change – CinCout Jun 30 '16 at 11:31
  • @DieterLücking Thanks for the comment. Is this "high standard" for compilers, or might that depend on situations (e.g. optimization level, platform...) – philkark Jun 30 '16 at 11:32
  • [FYI] If these are truly read only values then there is no harm in marking them const. It lets the compiler watch out for you accidentally modifying them. – NathanOliver Jun 30 '16 at 11:56

1 Answers1

1

I'm sorry because it is likely not to be the expected answer, but if the code WILL be compiled on "highly different platforms", you should not even try to do such low level optimization. As usual, carefully review all algorithms, profile the code (on one platform...) to see where you spend most of the time, and review twice those parts. If it is still not enough,write directly in assembly (one per supported target platform) the most critique parts. But for questions similar to yours, I now assume that the compiler will be smarter than a C++ programmer...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Thanks for the answer. And I (honestly) like the "I now assume that the compiler will be smarter than a C++ programmer." Since assembly programming is not really possible for me, I guess I will take it as it is. Is there a reason, though, why `const int` might be worse than `int`? – philkark Jun 30 '16 at 11:41