Why is the "in" operator with a tuple as a key in python so slow?

I have a dict, for example:

d=dict()
d[('1','2')] = 'value'

Then I request a key:

if (k1,k2) in d.keys():

When there are a million records, speed is misery, any problem with the in operator?

Is it a sequential search?

I need concat str as the key to getting around this problem.

+5
source share
2 answers

You have to use

(k1,k2) in d

instead of calling d.keys().

By doing this, Python 2 will lead to a linear search and most likely denies the benefits dict. In Python 3, your code is efficient (see comments below), but my version is clear.

+12
source

Nolen Royalty, , , timeit . dict , dict , .

3.2:

python -m timeit -s 'd = {(str(i), str(j)):"a" for i in range(100) for j in range(1000)}' '_ = ("1", "2") in d.keys()' '_ = (1, 2) in d.keys()'
1000000 loops, best of 3: 0.404 usec per loop

python -m timeit -s 'd = {(str(i), str(j)):"a" for i in range(100) for j in range(1000)}' '_ = ("1", "2") in d' '_ = (1, 2) in d'
1000000 loops, best of 3: 0.247 usec per loop

. 3.x, dict, 2x , .

2.7.3:

python2 -m timeit -s 'd = {(str(i), str(j)):"a" for i in range(100) for j in range(1000)}' '_ = ("1", "2") in d.keys(); _ = (1, 2) in d.keys()'
10 loops, best of 3: 36.3 msec per loop

python2 -m timeit -s 'd = {(str(i), str(j)):"a" for i in range(100) for j in range(1000)}' '_ = ("1", "2") in d' '_ = (1, 2) in d'
10000000 loops, best of 3: 0.197 usec per loop

2.x . dict.keys() 36 300 , dict 0,2 . .

, .

Edit:

, . , , :

python2 -m timeit -s 'd = {(str(i), str(j)):"a" for i in range(100) for j in range(1000)}' 'd.keys()' 'd.keys()'
100 loops, best of 3: 5.84 msec per loop

python2 -m timeit -s 'd = {(str(i), str(j)):"a" for i in range(100) for j in range(1000)}' -s 'l=d.keys()' '_ = ("1", "2") in l' '_ = ("1", "2") in l'
10 loops, best of 3: 25.3 msec per loop

, dict, , 1/6 , 5/6th .

+5

All Articles