Python 2 times slower than Perl

Mark Day mday at
Tue Jul 17 22:59:54 CEST 2001

In article <8f41cfd.0107171238.6ff33b9b at>, Xu, C.S.
<xucs007 at> 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.


More information about the Python-list mailing list