Re: SVN: Zope/trunk/ Optimized the `OFS.Traversable.getPhysicalPath` method to avoid excessive amounts of method calls. Thx to Nikolay Kim from Enfold

Tres Seaver <[email protected]>
Newsgroups gmane.comp.web.zope.devel
Message-ID <[email protected]>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 07/14/2011 11:47 AM, Tres Seaver wrote:

> While we're at it, 'list.insert(0,foo)' is known to be slower in a
> tight loop than 'list.append(foo)', with a 'reversed' at the end of
> the loop::
> 
> $ python -m timeit -s "from string import letters" "path = []" \ "for
> letter in letters: path.append(letter)" \ "result =
> tuple(reversed(path))" 100000 loops, best of 3: 15.9 usec per loop
> 
> $ python -m timeit -s "from string import letters" "path = []" \ "for
> letter in letters: path.insert(0, letter)" \ "result = tuple(path)" 
> 10000 loops, best of 3:  25.3 usec per loop
> 
> 
> For the sake of comparison, the original tuple addition is actually 
> between the two:
> 
> $ python -m timeit -s "from string import letters" "result = ()" \ 
> "for letter in letters: result += (letter,)" 10000 loops, best of 3:
> 21.6 usec per loop

A further micro-optimization is to pre-allocate a big list in a thread
local.  Running the attached script produces::

 $ python perftest.py
 Via insert_0: 24.8401839733
 Via append: 15.6596238613
   speedup: 37.0%
 Via tuple_add: 21.9555268288
   speedup: 11.6%
 Via prealloc: 10.5278339386
   speedup: 57.6%



Tres.
- -- 
===================================================================
Tres Seaver          +1 540-429-0999          [email protected]
Palladion Software   "Excellence by Design"    http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk4fIVAACgkQ+gerLs4ltQ6BlACguSHcHLWGjVIVJlQYIpL42RNc
mqMAn1Y16tyVmUkxd2ipNrZSeZpSFVEA
=oJJY
-----END PGP SIGNATURE-----

_______________________________________________
Zope-Dev maillist  -  [email protected]
https://mail.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists - 
 https://mail.zope.org/mailman/listinfo/zope-announce
 https://mail.zope.org/mailman/listinfo/zope )
perftest.py (application/x-httpd-cgi, 1.3 KB)
from string import letters
from threading import local

def via_append():
    path = []
    for letter in letters:
        path.append(letter)
    return tuple(reversed(path))
    
def via_insert_0():
    path = []
    for letter in letters:
        path.insert(0, letter)
    return tuple(path)

def via_tuple_add():
    result = ()
    for letter in letters:
        result += (letter,)
    return result

prealloc = local()
def via_prealloc():
    items = getattr(prealloc, 'items', None)
    if items is None:
        items = prealloc.items = [None] * 1000
    count = 0
    for letter in letters:
        items[count] = letter
        count += 1
    return tuple(items[:count])

if __name__ == '__main__':
    from timeit import Timer
    insert_0 = min(Timer(via_insert_0).repeat())
    print 'Via insert_0:', insert_0

    append = min(Timer(via_append).repeat())
    print 'Via append:', append
    print '  speedup: %0.1f%%' % ((insert_0 - append) / insert_0 * 100)

    tuple_add = min(Timer(via_tuple_add).repeat())
    print 'Via tuple_add:', tuple_add
    print '  speedup: %0.1f%%' % ((insert_0 - tuple_add) / insert_0 * 100)

    prealloc = min(Timer(via_prealloc).repeat())
    print 'Via prealloc:', prealloc
    print '  speedup: %0.1f%%' % ((insert_0 - prealloc) / insert_0 * 100)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.