Wednesday 10 October 2012

Short note on Java Stacks

Always use ArrayDeque instead of Stack because it is likely to be faster than a Stack when used as a stack (also faster than LinkedList when used as a queue, but this is obvious since LinkedLists need to allocate memory for each insert, c.f., array backed ArrayDeque).

ArrayDeque is backed by ArrayLists (which are backed by resizable arrays), as opposed to Stack, which is backed by Vectors (also backed by resizable arrays). Vectors are synchronized. The unsynchronized, and thus, faster, ArrayLists have made Vectors redundant. I expect this is where the gains come from by using ArrayDeque instead of Stack.

No comments:

Post a Comment