time complexity or hidden cost of .length in java

后端 未结 5 1316
失恋的感觉
失恋的感觉 2020-12-06 11:03

I was looking at a project in java and found a for loop which was written like below:

for(int i=1; i

        
5条回答
  •  北海茫月
    2020-12-06 11:26

    a.length is not a calculation but merely an access to a field held within the array. That type of read operation is super fast.

    If the code is part of a method which is called often enough, it is almost certain that the JIT compiler will do the optimisation you propose to make it even faster.

    The potential speed difference is in nanoseconds here (probably without an "s").

提交回复
热议问题