Is it better to use a pointer to navigate an array of values,or is it better to use a subscripted array name?
How does the compiler treat both approaches?
Is it better to use a pointer to navigate an array of values,or is it better to use a subscripted array name?
How does the compiler treat both approaches?
Any modern compiler should produce an equivalent assembly code for both methods.
I did a short test. I created int arr[10]
and set all cells to 10
using normal for loop indexed by int
and one using int*
.
What was strange form me (I accept Midhun MP arguments) pointer indexed loop assembly code was larger then normal approach (1 line more). But when I turn O3
optimization output was exactly the same.
IMO code should be easy to read and work without errors on the first place. Micro optimization can be done only if you really need them. In other cases readability beats performance.
If you are curious how it works. Just do this test yourself. Prepare 2 versions of code, compile it with gcc -S
and diff outputs.