2

Let's say I create an array of ints with length 10, i.e.

int[] array = new int[10];

At some point in my code, I want to compare the value of an int variable, let's call it var, with the length of the array.

I would like to know if this piece of code:

if(var == array.length) { // stuff }

and this piece of code:

if(var == 10) { // stuff }

which do exactly the same thing, have also the same performance.

In other words, I would like to know the internal mechanics that the JVM (?) uses to find the length of the array (I don't say "to return" since length is a field, not a method). Does it make use of iteration? Because if it does, then the 2nd piece of code would be faster than the 1st one.

EDIT: Similar question regarding array.length cost (even though focusing more to its use in for loops):

What is the Cost of Calling array.length

Community
  • 1
  • 1
PeterHiggs
  • 97
  • 2
  • 10
  • 9
    Never use the second one. Always use the first one. [Magic numbers are bad](http://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad). – Sotirios Delimanolis Feb 26 '14 at 17:15
  • The (JIT) compiler will optimze this out. – PeterMmm Feb 26 '14 at 17:16
  • @SotiriosDelimanolis In general you are right about that, but in my code this is not a problem, really. From a performance point of view? – PeterHiggs Feb 26 '14 at 17:16
  • I might be mistaken, but logic dictates that the field is updated when the array langth changes (;)). thus, a lookup for the array length doesn't need any iteration, for the field already contains the number. – npst Feb 26 '14 at 17:16
  • @CoolBeans I haven't check this because it is about 2-D array (i.e. a table). I will take a look, maybe I will find something there. – PeterHiggs Feb 26 '14 at 17:18
  • @VivinPaliath Ooops, yes, you are right, this question address the same issue! I will edit my question and put a link for that post. – PeterHiggs Feb 26 '14 at 17:24

4 Answers4

3

.length is a property, so it would not do iteration for sure. Still, the value of the property is, naturally, fetched at runtime, meaning that the second solution will be a little bit faster (as this is comparison with constant).

Still the first implementation is far more preferable:

  • This makes your code quite more maintainable
  • You can alter the length of the array only at one place
  • You will never feel the performance difference unless you pass through this if litterally millions of times in a second.

EDIT By the way you can yourself tell this is a property - there are no braces after the call. I at least do not know of a way in java to make property access do additional computation, but just retrieving its value.

Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • +1 for the good answer, it highlights all the aspects of the concept! Thank you! – PeterHiggs Feb 26 '14 at 17:21
  • @PeterHiggs Sure, thanks. Also please mind my edit I think it will help you get around similar cases in the future. – Boris Strandjev Feb 26 '14 at 17:22
  • Yes I will! I am new in programming and it happens from time to time to have questions that are simple to answer and have to do with fundamental concepts, but at least once I learn from my mistakes, I usually don't repeat them! :) – PeterHiggs Feb 26 '14 at 17:29
1

.length is a property of the array, not a function. Thus, the result would be available immediately, with no iteration necessary.

BVB
  • 5,380
  • 8
  • 41
  • 62
  • That was my initial thought, but I wanted to be sure, because I have some code that runs much slower than expected and I want to find out why! Well, array.length is not the problem apparently! Thank you! – PeterHiggs Feb 26 '14 at 17:19
  • @PeterHiggs any chance the Java chat room would like to help? – John Dvorak Feb 26 '14 at 17:20
  • @Jan Dvorak Yes, I think I will discuss about my performance issues and how to optimize the performance of my code on the Java chat. – PeterHiggs Feb 26 '14 at 17:32
  • @Jan Dvorak Already uploaded on the chat, you could take a look if you would like: https://filetea.me/t1sLLgTRgRbTGizbpbc5e1EYQ and https://filetea.me/t1seyU1k5P7Q5qxx1JMP9iKTg – PeterHiggs Feb 26 '14 at 18:58
1

From the Java Doc

The members of an array type are all of the following:

The public final field length, which contains the number of components of the array. length may be positive or zero.

length is an final field of array, so no iterations are required while writing following code.

if(var == array.length) { // stuff }

And it is good coding practice indeed.

Not a bug
  • 4,286
  • 2
  • 40
  • 80
0

The length property of an array is extracted in constant (O(1)) time - there is no iteration needed. It's also good practice to use this.

ucsunil
  • 7,378
  • 1
  • 27
  • 32