Perl code 37 times quickly than Python code??

Wesley J. Chun wesc at deirdre.org
Tue Jan 15 18:08:46 EST 2002


gaya at infomail.lacaixa.es (Toni Gaya) wrote in message news:<61f6bfa8.0201151153.1cd018ae at posting.google.com>...
> > python
> > ------
> > def Ramp(result, size, start, end):
> >     step = (end-start)/(size-1)
> >     for i in xrange(size):
> >         result[i] = start + step*i
> > 
> > def main():
> >     array = [0]*10000
> >     for i in xrange(100):
> >         Ramp(array, 10000, 0.0, 1.0)
> > 
> > main()
> 
> perl
> ----
> sub Ramp {
> 	local (*result, $size, $start, $end) = @_;
>     $step = ($end-$start)/($size-1);
>     for ($i=0; $i<$size; $i++)
> 	{
>         $result[$i] = $start + $step*$i;
> 	}
> }
> 
> sub main {
> 	for ($i = 0 ; $i < 10000 ; $i++) {
> 		push(@array, 0);
> 	}
> 	for ($i=0; $i<100; $i++) {
>         do Ramp(*array, 10000, 0.0, 1.0);
> 	}
> }
> 
> do main();
> 
> 
> Results in a PII 233 Mhz:
> [root at localhost source]# time python array.py
> 5.18user 0.00system 0:05.19elapsed 99%CPU (0avgtext+0avgdata
> 0maxresident)k
> 0inputs+0outputs (253major+153minor)pagefaults 0swaps
> 
> [root at localhost source]# time perl array.pl
> 0.14user 0.00system 0:00.14elapsed 98%CPU (0avgtext+0avgdata
> 0maxresident)k
> 0inputs+0outputs (261major+186minor)pagefaults 0swaps
> 
> I suppose two programs generate same result, is not?. I like Python,
> but Python program execution is 37 times long that Perl program. What
> is WRONG??


do you get the same results if you use range() instead of xrange()?
the reason why i ask is because the comparison is unequal.  xrange()
is much slower than range() because of its tendency towards memory-
friendliness in tighter environments.

-wesley

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

"Core Python Programming", Prentice Hall PTR, © 2001
    http://starship.python.net/crew/wesc/cpp/

wesley.j.chun :: wesc at deirdre.org
cyberweb.consulting : henderson, nv : cyberweb at rocketmail.com
http://www.roadkill.com/~wesc/cyberweb/



More information about the Python-list mailing list