Originally posted by: DaveSimmons
Bench and see if you really care, but it's trivial for both so not worth optimizing.
The rule of thumb really is true, > 90% of your code is not worth optimizing for speed.
Consider if the loop is serializing the elements to/from a file. Then 99.99999% of the the time will be spent on serializing, 0.00001% on moving to the next element.
Originally posted by: akubi
using the iterator is faster.
btw, I think your code is buggy if you remove those elements like that while you're iterating through it.
for example, if you remove(0) then everything after that is shifted down, but because your i++ you'll be doing remove(1), etc, and you won't end up removing all the elements.
in general, you don't want to remove stuff out of collections while iterating.
Originally posted by: randumb
Originally posted by: akubi
using the iterator is faster.
btw, I think your code is buggy if you remove those elements like that while you're iterating through it.
for example, if you remove(0) then everything after that is shifted down, but because your i++ you'll be doing remove(1), etc, and you won't end up removing all the elements.
in general, you don't want to remove stuff out of collections while iterating.
whoops, you're right, the removal wouldn't work with the index number implementation.
why would the iterator be faster? is it because get has to search through the arraylist each time and the iterator has pointers to the next object?
Originally posted by: SinNisTeR
results:
iterator: 63
index: 938
*keep in mind that scores can be skewed by other programs utilizing cpu time...
Originally posted by: znaps
The only difference I can see is your generics usage in the second part of the test to avoid an explicit cast. I did notice that the comparisons may not be equal, but I didn't think it would have made such a dramatic difference.
You might want to crank up the size of the ArrayList. I've not known java's timing facilities to be very fine grained at that level.Originally posted by: SinNisTeR
wow, that makes a big difference. I wonder why.
Now im getting
iterator: 62
index: 47
or often times, they are equal.![]()