[Zope3-dev] Re: [z3-five] Re: Patch for testbrowser.py

Paul Winkler pw_lists at slinkp.com
Fri Apr 21 12:23:55 EDT 2006


On Fri, Apr 21, 2006 at 04:07:43PM +0200, Daniel Nouri wrote:
> The relevant code in Zope2's ZPublisher.HTTPResponse.__str__:
> 
>         # ... we just built a headersl list using self.heders
>         if self.cookies:
>             headersl = headersl+self._cookie_list()
>         headersl[len(headersl):] = [self.accumulated_headers, body]
>         return '\n'.join(headersl)
> 
> Maybe this can shed some light on whether we want to include that patch.

As an aside:

What's with assigning to headersl[len(headersl):] instead of calling 
headersl.extend(...)?

I've seen this idiom a couple of times and I always wonder
why, it's much less readable IMO. 

I doubt it's supposed to be an optimization, since this code runs only
once per request; anyway, the two idioms are about equivalent in speed
AFAICT.

If it were in a performance-sensitive loop, a bit faster 
and still quite readable would be:

    headersl += [self.accumulated_headers, body]

timeit confirms this, here on python 2.4.2:

>>> import timeit
>>> slicer = timeit.Timer('x[len(x):] = [4,5,6]', 'x=[1,2,3]')
>>> adder = timeit.Timer('x += [4,5,6]', 'x=[1,2,3]')
>>> extender = timeit.Timer('x.extend([4,5,6])', 'x=[1,2,3]')
>>> n = 1000000)
>>> slicer.timeit(n)
2.6605889797210693
>>> extender.timeit(n)
2.5256669521331787
>>> adder.timeit(n)
1.9388060569763184

-- 

Paul Winkler
http://www.slinkp.com


More information about the Zope3-dev mailing list