Python 2 times slower than Perl

Mark Day mday at apple.com
Tue Jul 17 16:59:54 EDT 2001


In article <8f41cfd.0107171238.6ff33b9b at posting.google.com>, Xu, C.S.
<xucs007 at yahoo.com> wrote:

> #!/usr/bin/env python
> i = 2.5;
> while i < 1e7:
>         j = 2.5 * 2.5
>         i += 1
> print i, j

Notice that the assignment to j is a loop invariant.  It could be moved
outside the while loop without changing the result.  I'll bet the C and
Java compilers are in fact moving it outside of the loop for you, so
you're only timing the increment of i.  I think Perl may be
precomputing 2.5 * 2.5, so it may just be repeating the assignment of j
and not the multiplication.  I don't think Python will evaluate the
expression at "compile" time, nor move the assignment outside the loop.

-Mark



More information about the Python-list mailing list