Speed quirk: redundant line gives six-fold speedup

Jack Diederich jack at performancedrivers.com
Thu Aug 25 14:10:31 EDT 2005


On Thu, Aug 25, 2005 at 01:55:48PM -0400, Bill Mill wrote:
> Bill Mill wrote:
> > 
> > Pentium M 1.8 GHz Windows 2k. Here's the top of the profile results
> > for fast and slow on my machine (these won't look decent except in a
> > fixed-width font):
> > 
> <snip profiles>
> >
> > Interestingly, the test.py:36 line, which takes 45 seconds (!!) in the
> > slow version, does not appear at all in the fast profile. I can't
> > figure out why - both printed out their data, so template must have
> > been called somewhere.
> > 
> 
> OK, I'm getting somewhere now. When I replace:
> 
> template = ("  | %s %s %s | %s %s %s | %s %s %s |\n" * 3).join([" 
> +-------+-------+-------+\n"] * 4)
> 
> wtih:
> 
> template = """   | %s %s %s | %s %s %s | %s %s %s |\n
>   | %s %s %s | %s %s %s | %s %s %s |\n
>   | %s %s %s | %s %s %s | %s %s %s |\n
>   +-------+-------+-------+\n
>   | %s %s %s | %s %s %s | %s %s %s |\n
>   | %s %s %s | %s %s %s | %s %s %s |\n
>   | %s %s %s | %s %s %s | %s %s %s |\n
>   +-------+-------+-------+\n
>   | %s %s %s | %s %s %s | %s %s %s |\n
>   | %s %s %s | %s %s %s | %s %s %s |\n
>   | %s %s %s | %s %s %s | %s %s %s |\n
>   +-------+-------+-------+\n"""
> 
> Then the non-dummy version is faster than the dummy version (very
> slightly, presumably because it doesn't need to allocate 28 dummy
> variables).

I think this is just another tweaking that hits the magic spot (or
avoids the bad spot) in some lookup table somewhere.  Perhaps it
has to do with the number interned strings and some worst case
behavior? varnames are interened, so changing the number of globals
would change the number of interened strings as well.

-jackdied




More information about the Python-list mailing list