LOGBOOK

HELP

Quiz Entry - updated: 2026.11.26

A HashMap takes about 100 ns per put, at one million entries as at four million. Why is it still a bad choice for a hard real-time system, such as a machine controller?

Because $O(1)$ is only the average. The one put that triggers a rehash copies the whole table and can take hundreds of thousands of times longer, and a real-time system must guarantee every single response time.

A simple measurement shows both sides. Insert the integers 1, 2, 3, … into a Java HashMap and time the individual put calls:

  • A normal put takes around 100 ns, and that does not change between one, two or four million entries. That is $O(1)$ in practice. (Sequential Integer keys are an easy case: an Integer's hash code is its own value, so they never collide.)
  • At 0.75 · 2²⁰ ≈ 786,000 entries the load factor reaches 0.75 and the table doubles. That single put was measured at about 210,000 times slower than the usual ~117 ns. The next rehash, with twice as many entries to move, takes longer still.

Averaged over all inserts this spike disappears, which is exactly what amortized $O(1)$ promises. But a controller does not care about the average. If a relay has to switch within a fixed time so that a robot arm stops before it hits a wall, one late response is a failure.

So for hard real-time work, prefer structures with a guaranteed bound per operation (a balanced search tree gives $O(\log n)$ every time), or size the hash table up front so that it never has to rehash while running.

From Quiz: ADS / Sets, Maps and Hash Tables | Updated: Nov 26, 2026