Incredibly surprising python programming

Previous Entry Share Next Entry
String comparison speed
ev_genus wrote in pywtf
It looks like strings comparison have no influence to dict operations. I've measured how dict insertion time depends from size of string keys.

from time import time
from string import letters, digits
from random import choice

alphanum = letters + digits

def test(count, size):
    names = []
    for i in range(count):
        names.append(''.join(choice(alphanum) for j in range(size)))

    data = {}
    start = time()
    for i in range(1000000):
        data[choice(names)] = 1
    return time()-start

for j in range(6, 13):
    for i in range(4, 13):
        size = 2**i
        count = 2**j
        print "%-5d %-5d %s" % (count, size, test(count, size))

generates random results. Maybe my calculations is wrong?


Log in