Why it is so dramatical?

Bruce Ramsay brucer at theramsays.com
Mon Sep 2 20:35:19 EDT 2002


"Bo M. Maryniuck" <b.maryniuk at forbis.lt> wrote in message news:<mailman.1030962662.4274.python-list at python.org>...
> On Monday 02 September 2002 11:57, Duncan Booth wrote:
> > None of this is specific to Python, the same issues arise in almost any
> > programming language (although not always the same solutions).
> Almost... Keep in mind, that here is the range 0-0xffffff, what is quite
> bigger that 0-0xfff:
> ----------------------8<---------------------------
> #!/usr/bin/perl
> 
> $b = '';
> $data = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
> AAAAAAAAAAAAAAAAAAA';
> for $a (0..0xffffff) {
>     $b += $data;
> }
> ----------------------8<---------------------------
> 
> Since time() always show me zero, I've used UNIX time command:
> ----------------------8<---------------------------
> *bo at oberon:(~/work/spike/benchmarks) time perl str vs list.pl 
> 
> real    0m8.734s
> user    0m8.710s
> sys     0m0.010s
> ----------------------8<---------------------------
> 
> As you see, adding string to the string isn't a huge problem for Perl as 
> for Python.
> What's "wrong" with Perl?

The Perl += operator does not concatenate strings.

Bugs aside, I suspect that Perl has an optimisation for this case, or
a special string data structure.  Perl run times seem to grow linearly
for loop counts from 1e5 to 1e7.  Python run times go from 0.053s to
18.2s when changing the loop count from 1e3 to 1e4.  This is on Python
2.1 on an Athlon 1600.



More information about the Python-list mailing list