[Python-Dev] Re: Fast access to __builtins__

Raymond Hettinger python@rcn.com
Fri, 28 Mar 2003 18:05:17 -0500


[Raymond]
> > I've already tried out a pure python proof-of-concept 

[Michael Chermside]
> Does that mean that you can give us some idea what kind of
> performance boost this actually resulted in?

It depends on what you're timing but it is not a big win.

* Speed doubles in demo code that just makes references to globals
   but is much more modest when the builtins are called.  This shows
   that the call time is more significant than the reference time:

        def f(i):
            dict; hasattr; float; pow; list; range   # speed more than doubles
            hex(i); str(i); oct(i); int(i); float(i)    # 12% gain

*  Contrived examples show the best gains while code from real apps 
    show smaller improvements:

    def shuffle(random=random.random):     # 6% gain
        x = list('abcdefghijklmnopqrstuvwyz0123456789')
        for i in xrange(len(x)-1, 0, -1):
            j = int(random() * (i+1))
           x[i], x[j] = x[j], x[i]

* PyStone does not use any builtins.

* Scanning my own sources, it looks like some of the builtins
   almost never appear inside loops (dir, map, filter, zip, dict, range).
   The ones that are in loops usually do something simple (int, str,
   chr, len).  Either way, builtin access never seems to dominate
   the running time.  OTOH, maybe that's just the way I write code.


Raymond Hettinger