I\'m parsing some xml (with some python 3.4 code) and want to retrieve both the text from a node and its id attribute. Example:
No, Python string length hardly has an impact on dictionary performance. The only influence the string length could have is on the hash()
function used map the key to a hash table slot.
String length has very little impact on the performance of hash()
:
>>> import random
>>> from timeit import timeit
>>> from string import ascii_letters
>>> generate_text = lambda len: ''.join([random.choice(ascii_letters) for _ in xrange(len)])
>>> for i in range(8):
... length = 10 + 10 ** i
... testword = generate_text(length)
... timing = timeit('hash(t)', 'from __main__ import testword as t')
... print 'Length: {}, timing: {}'.format(length, timing)
...
Length: 11, timing: 0.061537027359
Length: 20, timing: 0.0796310901642
Length: 110, timing: 0.0631730556488
Length: 1010, timing: 0.0606122016907
Length: 10010, timing: 0.0613977909088
Length: 100010, timing: 0.0607581138611
Length: 1000010, timing: 0.0672461986542
Length: 10000010, timing: 0.080118894577
I stopped at generating a string of 10 million characters, because I couldn't be bothered waiting for my laptop to generate a 100 million character string.
The timings are pretty much constant, because the value is actually cached on the string object once computed.