[MERGE] simple performance improvement

Andrew Bennetts andrew at canonical.com
Wed Jan 31 23:28:36 GMT 2007


Robert Collins wrote:
> On Wed, 2007-01-31 at 11:49 -0600, John Arbash Meinel wrote:
> > Counter intuitively, I have evidence that changing the line:
> > 
> > f = open(name, 'wb')
> > 
> > to
> > 
> > def do_open():
> >   return open(name, 'wb')
> > f = do_open()
> > 
> > Actually shaves off another 20ms. (down to 4.19) This is averaged over
> > 15 runs. So while it isn't a huge dataset, it isn't like I just ran it a
> > couple times.
> > 
> > I don't really know how to respond to that, I only did it because
> > --lsprof doesn't show the time spent in open(), but it does show the
> > time spent in a nested function like do_open().
> 
> It may well be because do_open is local, so is bound in the local
> variables, whereas open is a global, so has to be looked up. If we do
> a /lot/ of opens that could matter.

But then do_open has to do the exact same global lookup of 'open' that was being
done anyway, *plus* the overhead of calling a python function (which is
expensive because of the cost of setting up the frame object).  So this result
makes no sense to me.

John, how certain are you that this effect is real, and not just an artefact of
using lsprof?

If you want to avoid the expense of a global lookup, put "open=open" as a
default arg of the function, or use Raymond Hettinger's make_constants decorator
(http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277940).  It might be
interesting to compare the timing of this versus the other options.

-Andrew.




More information about the bazaar mailing list