performance problem in python 2.2
Mark Day
mday at apple.com
Fri Jul 26 20:25:07 EDT 2002
In article <ahsh4t$i13$1 at peabody.colorado.edu>, Fernando Perez
<fperez528 at yahoo.com> wrote:
> As you see, perl/python are in the same league, even though perl is a factor
> of 2 faster.
Maybe you missed Tim's comment about putting the loop in a function to
make it faster. Using that trick made the Python version as fast as
Perl on my machine.
-Mark
% time ./p1.py 1000000
2.71050724088e-08
./p1.py 1000000 2.76s user 0.06s system 83% cpu 3.375 total
% time ./p2.py 1000000
2.71050724088e-08
./p2.py 1000000 1.88s user 0.07s system 82% cpu 2.369 total
% time ./p3.pl 1000000
2.71050724087729e-08
./p3.pl 1000000 1.90s user 0.01s system 85% cpu 2.246 total
% cat p1.py
#!/usr/bin/env python
import sys
n = 1.0
p = 2.0**64 # making p a float for fariness of comparison, not
correctness
c = int(sys.argv[1])
for i in xrange(1,c+1):
n = (n * (p-i)) / p
print 1-n
% cat p2.py
#!/usr/bin/env python
import sys
def foo(arg):
n = 1.0
p = 2.0**64 # making p a float for fariness of comparison, not
correctness
c = int(arg)
for i in xrange(1,c+1):
n = (n * (p-i)) / p
print 1-n
foo(sys.argv[1])
% cat p3.pl
#!/usr/bin/perl
$p = 2**64;
$c = $ARGV[0];
$n = 1;
foreach $i (1..$c) {
$n = ($n * ($p-$i)) / $p
}
print 1-$n, "\n";
More information about the Python-list
mailing list