1

I need to know if my arraylist has 10 slots or not. Would it be a faster function to call for the size, or just as for the 10th index? Also is there any kind of way for me to check the speed of certain functions? Kind of like how phpMyAdmin can show you the time it takes to do a query.

if(strideList.size() == 10)
{

}

OR

if(strideList.get(9) != null)
{

}
gmustudent
  • 2,229
  • 6
  • 31
  • 43
  • 1
    I think this is relevant [link](http://stackoverflow.com/questions/180158/how-do-i-time-a-methods-execution-in-java) – Ken Mar 03 '13 at 03:48
  • Out of curiosity, why is it so important to have this size check go as quickly as possible? Moreover, why is it worth sacrificing the ability to ensure that the size is *exactly* 10 in order to get the speed gain? – Kyle Strand Mar 03 '13 at 04:23

3 Answers3

4

You should use list.size() because that clearly shows your intent. The other piece of code is actually wrong what if the list has 20 elements? strideList.get(9) != null just checks if the ninth element is different than null.

As for checking the speed of functions, you can use timers to calculate the amount of time passed from the beginning to the end of a function or use special software called profilers to see which functions/code paths are slow in your application.

Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66
  • there is no way the list could have more then 10 elements. I populate the list within a loop. knowing this would the latter be the best option? – gmustudent Mar 03 '13 at 03:54
  • No I would still use the easier to read version `size`. Don't worry about optimizing your code until you know that this piece is the bottleneck. Make it work, then make it readable, then if it is not fast enough for the job optimize. – Barış Uşaklı Mar 03 '13 at 03:58
4

Based on the ArrayList.java source (other List implementations will be similar), size() just returns an int member variable from the object. get(), however, does a range check, goes through 3 or 4 functions of overhead, then does an array access which is going to take even more processing power.

Use size().

(Plus, as @Barış Uşaklı said, it's more readable with size().)

Community
  • 1
  • 1
Cat
  • 66,919
  • 24
  • 133
  • 141
1

The strideList.get(9) will throw a IndexOutOfBoundsException and will take more time than list.size() However point to be noted is the code for get(int index). There are two steps involved 1. rangeCheck which just matches the size which you get out of size() 2. If the range is greater than size , it throws the Exception,which is additional time taken 3. If the size is not greater than size , then it will get from Array. Code for ArrayList :

    public E get(int index) {
    382           rangeCheck(index);
    383   
    384           return elementData(index);
    385       }

  private void rangeCheck(int index) {
   603           if (index >= size)
   604               throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
   605       }
   606   


     E elementData(int index) {
      371           return (E) elementData[index];
      372       }
      373   
user1428716
  • 2,078
  • 2
  • 18
  • 37