list math for version 1.5.2 without Numeric

Andrew Dalke dalke at dalkescientific.com
Thu Nov 29 11:44:35 EST 2001


J.Jacob:
>Oh I forgot that functions can be assigned to variables in Python,
>elimination lookup.  As a dutch saying has it I was "comparing apples
>with pears".
>
>version_6:
>        add = operator.add
>        mul = operator.mul
>        for j in range_nh:
>            wij = wi[j]
>            sum = reduce(add, map(mul, ai, wij))
>Takes 71 seconds

The 'reduce' and 'map' are doing a lookup in builtins, which
is much slower than doing a local variable lookup.  Try this:

       add = operator.add
       mul = operator.mul
       local_reduce = reduce
       local_map = map
       for j in range_nh:
           wij = wi[j]
           sum = local_reduce(add, local_map(mul, ai, wij))

Also, the j index is only use to look up elements from wi,
so you can replace that with 'for wij in wi'

       add = operator.add
       mul = operator.mul
       local_reduce = reduce
       local_map = map
       for wij in wi:
           sum = local_reduce(add, local_map(mul, ai, wij))

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list