3 ms·
Apparently there's a function call overhead as well that the % operator doesn't incur because for i in xrange(10000000): s = "%s" % i is also faster tha
by fauigerzigerk 14y ago
Apparently there's a function call overhead as well that the % operator doesn't incur because
for i in xrange(10000000):
s = "%s" % i
is also faster than
lstr = str
for i in xrange(10000000):
s = lstr(i)
[Edit] After thinking about it a second longer, I wonder whether there is some lookup for lstr as well even though it's local. But storing the function in lstr is faster than using str so I'm not sure how this is actually implemented. I'm sure someone here will know more.
- njharman 14y agoYes there is lookup, always lookup. Dynamic language, something in loop could change what lstr is. Each "dot" incurs lookup. Your example reminds me of one idiom. Assigning nested.look.up to local var for access inside loop.
- dmorgan 14y ago>Yes there is lookup, always lookup. Dynamic language, something in loop could change what lstr is. And why is that presented as something inevitable? The interpreter/compiler could analyze that part and see that the function/name is not changed during the loop, and cache for that. I'd guess that PyPy tries to do it that way, anyway...
- lvh 14y agoYes, that's one of the many optimizations PyPy does, however it's only a very limited one. PyPy goes much, much deeper than this, with tools like function inlining and escape analysis.
- jemfinch 14y agoYou need to make sure you do all your benchmarks in a function. In a funtion, local variables translate to indexes into a local variable array; outside of a function, they remain globals lookups. Your first reply that "there's always a lookup" isn't right. Some variable accesses (specifically: local variable accesses) do simply map to array accesses.
- pjscott 14y agoCPython compiles to bytecode, turning all local variable references into offsets into a local variable array. So, getting the "str" function requires a global name lookup, while getting your local "lstr" only requires an array lookup with a constant embedded in the bytecode. You can have a lot of fun playing with the dis module to look at the bytecode that Python generates for your functions: http://docs.python.org/library/dis.html http://docs.python.org/library/dis.html