os.listdir

Michael Peuser mpeuser at web.de
Thu Sep 11 12:42:56 EDT 2003


"Duncan Booth" <duncan at NOSPAMrcp.co.uk>

[highly interesting details from Duncun]

Where did you get all these details - reading Python compiler source code?
Because it sounded somehow there *could* be degradings when going up to
large dicts, I benchmarked it - no noticable effects (see below). As one can
see as well there is not much disadvantage over list accesss...
Kindly
Michael P

from random import randrange,shuffle
from time import clock

N = 10000
hash={}

t0=clock()
for i in xrange(N):
    hash[long(i**2)]=4711

checkKeys=hash.keys()
shuffle(checkKeys)
print "prepare",N, clock()-t0

t0=clock()
for i in checkKeys[:10000]:
    x=hash[i]
print "access from ",N, clock()-t0
hash=None

N = 1000000
hash={}

t0=clock()
for i in xrange(N):
    hash[long(i**2)]=4711 #this takes some time ;-)

checkKeys=hash.keys()
shuffle(checkKeys)  # this probably too
print "prepare",N, clock()-t0

t0=clock()
for i in checkKeys[:10000]:
    x=hash[i]
print "access from ",N, clock()-t0
hash=None

t0=clock()
for i in checkKeys[:10000]:
    pass
print "empty loop", clock()-t0






More information about the Python-list mailing list