[pypy-dev] Python vs pypy: interesting performance difference [dict.setdefault]
David Naylor
naylor.b.david at gmail.com
Wed Aug 10 21:27:10 CEST 2011
Hi,
I needed to create a cache of date and time objects and I wondered what was the best way to handle the cache. For comparison I put together
the following test:
<file="iforkey.py">
import datetime
import random
import timeit
ranges = [datetime.datetime(2011,01, random.randint(1, 31)) for i in xrange(1000)]
def ifdict():
cache = {}
b = []
for i in ranges:
key = i.day
if key in cache:
b.append(cache[key])
else:
date = i.date()
cache[key] = date
b.append(date)
def keydict():
cache = {}
b = []
for i in ranges:
key = i.day
try:
b.append(cache[key])
except KeyError:
date = i.date()
cache[key] = date
b.append(date)
def defaultdict():
cache = {}
b= []
for i in ranges:
b.append(cache.setdefault(i, i.date()))
print "ifdict:", timeit.repeat("ifdict()", "from __main__ import ifdict", number=10000)
print "keydict:", timeit.repeat("keydict()", "from __main__ import keydict", number=10000)
print "defaultdict:", timeit.repeat("defaultdict()", "from __main__ import defaultdict", number=10000)
</file>
# python iforfile.py
ifdict: [2.432887077331543, 2.4002890586853027, 2.397233009338379]
keydict: [2.3483030796051025, 2.358638048171997, 2.314802885055542]
defaultdict: [3.5384328365325928, 3.5329859256744385, 3.5728111267089844]
# pypy iforfile.py (pypy 1.5)
ifdict: [0.8129069805145264, 0.74648118019104, 0.7432689666748047]
keydict: [0.5187451839447021, 0.4662129878997803, 0.4504108428955078]
defaultdict: [37.98510789871216, 37.859113931655884, 37.92770600318909]
Pypy displays significant slowdown in the defaultdict function, otherwise displays its usual speedup. To check what is the cause I replaced i.date()
with i.day and found no major difference in times. It appears dict.setdefault (or it's interaction with jit) is causing a slow down.
Regards
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20110810/f833eb4f/attachment-0001.pgp>
More information about the pypy-dev
mailing list