Re: fancy sorting



On Wed, 2004-03-31 at 23:32, muppet wrote:
Dan Lyke said:
It's my impression that

   for my $i (0..65535)

will create a 65536 element list before iterating $i through it. If
that's the case, why prefer that form over the C style?

*I* originally preferred it because I find it way more readable.

perl actually optimizes that specific structure to avoid creating the huge list.

Some hard ... ahem ... scientific data to back that up:

  #!/usr/bin/perl
  use strict;
  use warnings;
  use Benchmark;
  
  timethese(1_000, {
    C => sub {
      for (my $i = 0; $i < 65536; $i++) { }
    },
    Perl => sub {
      for my $i (0 .. 65535) { }
    }
  });
  __END__

  Benchmark: timing 1000 iterations of C, Perl...
           C: 24 wallclock secs (23.29 usr +  0.07 sys = 23.36 CPU) @
42.81/s (n=1000)
        Perl: 14 wallclock secs (14.35 usr +  0.07 sys = 14.42 CPU) @
69.35/s (n=1000)

Bye,
-Torsten




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]