# This function looks pretty innocent def sum_of_values(d): return sum(d.values()) # Should run in O(len(d)), and we will only call with len(d) == 1 # Timing: import timeit def prepare_bad_example(N): d = {i: i for i in range(N)} for i in range(N-2, -1, -1): del d[i] return d for N in [100000, 200000, 300000, 400000]: d = prepare_bad_example(N) print(N, timeit.timeit(f"sum_of_values(d)", number=10000, globals={"sum_of_values": sum_of_values, "d": d})) # Output on my machine: # # # 100000 0.3195033100055298 # 200000 0.6416430359968217 # 300000 1.0254385019943584 # 400000 1.3816945559956366