On Tue, Oct 20, 2009 at 11:33:13AM -0700, Danek Duvall wrote:
> [email protected] wrote:
> > My concern here was that flist may be arbitrarily long.  Iterating over
> > the list when there are no failures seemed wasteful.  I don't have a
> > problem with changing the way the test is performed if you're opposed to
> > the len() call.
> 
> I think that the test for inclusion in an empty list should be fast enough
> not to make any material difference here, and the code will be a bit more
> natural as a result.

I consider the case where failures are encountered to be the slow path
in this code.  While I agree that it's generally good to simplify the
code, what you're proposing causes the common path to become much slower
just for the sake of style.

As a test, I configured a list of 5000 integers.  The first test
simulates creating the list with a comprehension, checking against
another empty list L.

The second test just checks if L is empty, and then re-assigns the
list's reference, which is what happens in the fast path today.

The third test is a variation of the first, but it uses filter() instead
of a list comprehension.

----- >8 cut here 8< -----

#!/usr/bin/python

#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

from timeit import Timer

def test1():
        """Test list comprehension empty"""
        L = []
        lst = [ x for x in arry_req if x not in L ]

def test2():
        """Test conditional"""
        L = []
        if not L:
                lst = arry_req

def test3():
        """Test filter"""
        L = []
        lst = filter(lambda x: x not in L, arry_req)

if __name__ == '__main__':

        arry_req = range(5000)

        t1 = Timer("test1()", "from __main__ import test1,arry_req")
        print t1.timeit(10000)

        t2 = Timer("test2()", "from __main__ import test2,arry_req")
        print t2.timeit(10000)

        t3 = Timer("test3()", "from __main__ import test3,arry_req")
        print t3.timeit(10000)

----- >8 cut here 8< -----

test1 - 3.78901004791
test2 - 0.00216197967529
test3 - 6.96152305603

It looks like transport should switch to using list comprehensions
instead of filter, because it's 2x faster.  The difference between
performing the list comprehension when we don't have to and simply
checking if the list is empty is about 1800x.  I'm inclined to switch
the slow case to list comprehensions and leave the conditional in place.

-j
_______________________________________________
pkg-discuss mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/pkg-discuss

Reply via email to