Python wrapper for C++ STL?
Alex Martelli
aleaxit at yahoo.com
Mon Dec 11 08:00:50 EST 2000
"Andrew Dalke" <dalke at acm.org> wrote in message
news:90s9s1$5oj$1 at slb6.atl.mindspring.net...
>
> Rainer Deyke wrote
> >I tend to use a dictionary where all values are None for sets.
>
> For a bit more performance, use a number, like 0 or 1.
> Using None requires a name lookup.
Nice point! Copying None to a local variable (when you
know you'll need it a zillion times in a loop) may
ameliorate some of this, and the ability to check with
'is' rather than with '==' may give a tiny advantage:
-- finti.py:
def acheck_oneis(r):
for c in r:
if c is 1: return
import time
r = range(2,1000000)
for fun in (acheck_noneeq, acheck_noneis,
acheck_noneleq, acheck_nonelis,
acheck_oneeq, acheck_noneis):
t1=time.time();fun(r);t2=time.time()
print fun.func_name, t2-t1
-- end of finti.py
D:\PySym>python finti.py
acheck_noneeq 2.41399991512
acheck_noneis 2.05300009251
acheck_noneleq 2.01300001144
acheck_nonelis 1.73199999332
acheck_oneeq 1.75299990177
acheck_noneis 2.0830000639
D:\PySym>python finti.py
acheck_noneeq 2.41399991512
acheck_noneis 2.03299999237
acheck_noneleq 1.98200011253
acheck_nonelis 1.68299996853
acheck_oneeq 1.72200000286
acheck_noneis 2.04299998283
D:\PySym>
I'm not sure why 'is' should be measurably
faster for None and myNone, while being a
bit slower for 1 -- maybe it's crucially
dependent on the values one is testing. But,
at least on my machine, using a local variable
set to None, and testing with 'is', seems to
be a WEE bit faster than the second-best way,
using 1 and testing with '=='.
Alex
More information about the Python-list
mailing list