...

/

Slowing Down dict Lookups

Slowing Down dict Lookups

We'll cover the following...

Let’s show you how to slow down dict lookup at will.

Press + to interact
some_dict = {str(i): 1 for i in range(1000000)}
another_dict = {str(i): 1 for i in range(1000000)}
>>> timeit.timeit("some_dict['5']", number=1000, globals=globals())
6.216699989636254e-05
>>> some_dict[1] = 1
>>> timeit.timeit("some_dict['5']", number=1000, globals=globals())
9.339500002170098e-05
>>> timeit.timeit("another_dict['5']", number=1000, globals=globals())
6.14590001077886e-05
>>> another_dict[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 1
>>> timeit.timeit("another_dict['5']", number=1000, globals=globals())
8.72460000209685e-05

Try it out in the terminal below:

Terminal 1
Terminal
Loading...

Why are the same lookups becoming slower?

Explanation

  • CPython has a generic dictionary lookup function
...