Lisp to Python translation criticism?

John E. Barham jbarham at jbarham.com
Sat Aug 17 00:07:04 EDT 2002


"Andrew Henshaw" wrote:

> Should that last line be
>
>         return prod / (prod + inv_prob)
>
> ?

Yeah, it should.

> Probably not a good idea to have such similar variable names.

No, as demonstrated above.  I just thought it made it easier to compare w/
the original Lisp.

> On my machine, the fragment
>
>     inv_prob = 1.0
>     for prob in inv_probs:
>         inv_prob = inv_prob * prob
>
> takes about 50% more time to execute, than
>
>     inv_prob = reduce(operator.mul, inv_probs)

That's what I was looking for...

> for inv_probs of length 10.  The advantage to this code increases as the
> length of the list increases.  That's one local optimization that could be
> made.
>
> I'd say that you would increase both clarity and speed by collapsing the
> three loops in spam_prob into one loop, as
>
> def spam_prob(probs):
>     inv_prob = prod = 1.0
>     for prob in probs:
>         prod     *= prob
>         inv_prob *= (1 - prob)
>     return prod / (prod + inv_prob)

Good optimizations all.  Thanks!

    John





More information about the Python-list mailing list