java - Is using an int and then incrementing it slower than doing both simultaneously? -
i ran old source code recently, , noticed pattern this:
t item = array[index]; index++;
would faster express as
t item = array[index++];
? until thought there wouldn't difference, since thought shorthand index = index + 1
, either way dereferencing index
twice. this post has made me think otherwise; instead, getting value of index
once.
would yield difference (however small) in performance, or modern jits optimize away?
most moderns langages optimise during compile or execute time. simple things directly optimised during compilation.
but note exemple typo question, in 2 cases java understand t item = array[index++];
:
- get object in index place
- add 1 index
that's same thing writting
t item = array[index]; index++;
(but not same thing t item = array[++index];
equivalent :
index++; t item = array[index];
edit: can add that's loops improved too, compile or execute time exemple in c or c++
for(int = 0; < 1000; i++) { t[i] = 0 }
will become equivalent to
for(int = 0; < 1000; i++) { *(t++) = 0 }
to conclude, compiler , virtual machine improve code, think more algorithms , data structures chosen.
Comments
Post a Comment