Next: 1.5.2 Using stacks as Up: 1.5 Stacks Previous: 1.5 Stacks Contents Index

## 1.5.1 Efficiency issues

Stacks are implemented such that accessing an object is a constant-time operation, regardless of its depth in a stack. However, only the top and bottom of a stack may be modified in constant time.

Typically, the cost of stack manipulations is proportional to the number of objects being touched. So, rolling the top ten objects is five times as costly as exchanging the top two objects. Likewise, rotating the stack by ten positions is typically five times as expensive as rotating the stack by two positions. Costs for rolling and rotation are approximately proportional to each other, though pathological cases for rotation incur a slightly higher overhead (a constant factor more expensive, when amortized).

Stacks have more memory and performance overhead than arrays, so unless growing or shrinking is imporant, arrays are a better choice for indexed access. Dictionaries are always a better choice for keyed access.

Next: 1.5.2 Using stacks as Up: 1.5 Stacks Previous: 1.5 Stacks Contents Index