0

I have been looking deeper into different collection implementations and i wonder if there is any performance difference when implementing iterator on various List types(Like the previously mentioned in the title).

As we all know iterating over ArrayList is faster as opposed to LinkedList. However is the case same when we iterate over both using the iterator methods?

Also the case is reversed when adding and removing elements, then the LinkedList is faster, but if we implement listIterator which has better performance on the add/remove operations or do they equate for both lists and there is no difference.

I apologize if this question has been asked before, I wasn't able to find anything to help me.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Alex Vulchev
  • 201
  • 1
  • 2
  • 15
  • 1
    @HovercraftFullOfEels - Or even when not removing. I wouldn't expect an `ArrayList` iterator to be faster than a `LinkedList` iterator. Not *slower*, but not faster, not in any significant way. – T.J. Crowder Jan 12 '20 at 14:04
  • @T.J.Crowder less memory perhaps – Hovercraft Full Of Eels Jan 12 '20 at 14:05
  • 1
    @T.J.Crowder depends on the inlining of the JIT. The fundamental operation of an `ArrayList` iterator *is* simpler, as it doesn’t need to fetch a next node pointer and has better memory locality when reading the element pointer from an array rather than different node instances. The better the JIT optimizes the iterator using code, the more the `ArrayList` will win (in the absence of modifications). – Holger Jan 13 '20 at 18:07
  • @Holger - And yet I stand by "not in any ***significant*** way." :-) – T.J. Crowder Jan 13 '20 at 18:08
  • 1
    @T.J.Crowder when I consider a large list, oop size of four bytes, and a cache line size of 64, the memory locality may impose a factor of 16. Of course, whether this is significant, depends on what you are actually doing in the loop… – Holger Jan 13 '20 at 18:12

0 Answers0