1

There is some abstract loop that loops through array, and processes every single element without dependency of it's place in array. Element value is checked against conditions many times.

Adding int value = arr[i] will help reading the code, and theoretically, will take sizeof(int) memory away.

Will it boost performance? Or will compiler optimize it?

I know, early-optimization is very very bad, that is theoretical question.

int arr[10];
...
for (int i = 0; i < 10; i++)
{
   if (arr[i] > 2) 
   {
     if (arr[i] < 100)
     {
       do_smth1(arr[i]);
     }
     else if (arr[i] > 500 && arr[i] < 1000)
     {
       do_smth2(arr[i]);
     }
     else
     {
       do_smth3(arr[i]);
     }
   }
   else 
   {
     do_smth4(arr[i]);
   }
}
filimonic
  • 3,988
  • 2
  • 19
  • 26
  • 1
    Well, if we assume a strict unoptimized abstract machine flow, then each `arr[i]` will get translated into a pointer arithmetic operation. So from this perspective performing it only once and assigning the result to a temporary variable is preferable. But of course, even older compilers are smart enough to optimize it by themselves. – Eugene Sh. Jan 13 '23 at 19:49
  • 2
    Optimise for correctness and clarity. The token `element` is easier on the eyes than `arr[i]`... Eyeballs cope with "words" better than they do with "constructs". Are all those "arr" or is one of them "arrr"? In the case of `[i][j]` there've been SO questions because the bug `[i][i]` was very difficult to **see** amongst all the clutter. (Suggest using variable named `value` instead of `element` that could mean "index".) – Fe2O3 Jan 13 '23 at 20:12
  • 1
    If there is anything else that could change `arr` contents in the loop, such as an `*p = foo` where `p` is an `int *` that might somehow point into `arr`, then the compiler cannot know whether `arr[i]` changes at various places inside the loop, so it must reload it from memory. If you start the loop with `int element = arr[i];` and then use `element` throughout, the compiler will know that `element` cannot change. Thus, using `int element = arr[i];` can serve to enable some optimizations. – Eric Postpischil Jan 13 '23 at 20:25

1 Answers1

3

Will it boost performance?

No.

Or will compiler optimize it?

Yes. If not, get a better compiler.

... and theoretically, will take sizeof(int) memory away.

No, not certainly.


With such candidate linear optimizations, coding for clarity is overwhelming the best option.

Use your valuable coding time to improve bigger picture items and let the compiler handle the small stuff.

See also Is premature optimization really the root of all evil?.


Warning: Compilers today recognize many common idioms and emit efficient code. Should you code in an uncommon fashion, in order to "improve" things, the compiler may miss optimizations and then your "improved" code is worse than clear common code.

Instead of trying to outthink the compiler, use it to leverage your productively.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256