in reply to Re: Generating an array of n identical elements
in thread Generating an array of n identical elements

WOW! Good point, Blake!

Does anyone have any benchmark of the two solutions?

Ciao!
--bronto

# Another Perl edition of a song:
# The End, by The Beatles
END {
  $you->take($love) eq $you->made($love) ;
}

  • Comment on Re: Re: Generating an array of n identical elements

Replies are listed 'Best First'.
Re: Re: Re: Generating an array of n identical elements
by BrowserUk (Patriarch) on Sep 16, 2002 at 10:14 UTC

    Not certain that its exactly a great Benchmark, but it seems to clearly indicates that using the x op is substantially faster.

    #! perl -sw use strict; use Benchmark qw(cmpthese); sub mapgen { return map $_[0], (1..$_[1]); } sub xgen { return ($_[0]) x $_[1]; } cmpthese( 1000, { mapgen => 'my @ones = mapgen 1, 1000;', xgen => 'my @ones = xgen 1, 1000;', }); __END__ Benchmark: timing 1000 iterations of mapgen, xgen... mapgen: 9 wallclock secs ( 8.18 usr + 0.00 sys = 8.18 CPU) @ 12 +2.23/s (n=1000) xgen: 5 wallclock secs ( 5.15 usr + 0.00 sys = 5.15 CPU) @ 19 +4.29/s (n=1000) Rate mapgen xgen mapgen 122/s -- -37% xgen 194/s 59% -- C:\test>

    Well It's better than the Abottoire, but Yorkshire!

      Right. And using fixed values instead of generic subroutines appears to be much more (and more and more) fast:

      #! perl -sw use strict; use Benchmark qw(cmpthese); my $mapgen = sub { return map 5, (1..1000); } ; my $xgen = sub { return (5) x 1000 ; } ; cmpthese( 10000, { mapgen => $mapgen, xgen => $xgen, }); __END__

      gives, on my lightly-loaded Pentium IV 1.5Ghz,

      Benchmark: timing 10000 iterations of mapgen, xgen... mapgen: 5 wallclock secs ( 5.04 usr + 0.03 sys = 5.07 CPU) @ 19 +72.39/s (n=10000) xgen: 0 wallclock secs ( 0.03 usr + 0.00 sys = 0.03 CPU) @ 33 +3333.33/s (n=10000) (warning: too few iterations for a reliable count) Rate mapgen xgen mapgen 1972/s -- -99% xgen 333333/s 16800% --

      So map loses again. Uhm... it's the second time that other functions are far faster than map... I'd be interested in seeing a real case where map is the faster choice... does anyone have any?

      Ciao!
      --bronto

      # Another Perl edition of a song:
      # The End, by The Beatles
      END {
        $you->take($love) eq $you->made($love) ;
      }

        my @new = map { transform $_ } @old; is faster than
        my @new; push @new, transform $_ for @old;
        I'm wary of your results though. My gut feeling is that something is getting optimized away at compile time, but I can't seem to get it deparsed like that. Maybe x is really 168x faster than map in this case (and in your perl build and on your machine)..

        Makeshifts last the longest.

Re^3: Generating an array of n identical elements
by Aristotle (Chancellor) on Sep 16, 2002 at 10:54 UTC
    One for largish and one for small lists:
    $ perl -MBenchmark -e'timethese(10000, { map => sub { @_ = map 1, 1..1 +000 }, x => sub { @_ = (1)x1000 }})' Benchmark: timing 10000 iterations of map, x... map: 29 wallclock secs (27.09 usr + 0.24 sys = 27.33 CPU) @ 36 +5.90/s (n=10000) x: 14 wallclock secs (13.36 usr + 0.09 sys = 13.45 CPU) @ 74 +3.49/s (n=10000) $ perl -MBenchmark -e'timethese(1000000, { map => sub { @_ = map 1, 1. +.5 }, x => sub { @_ = (1)x5 }})' Benchmark: timing 1000000 iterations of map, x... map: 17 wallclock secs (15.55 usr + 0.15 sys = 15.70 CPU) @ 63 +694.27/s (n=1000000) x: 9 wallclock secs ( 8.44 usr + 0.11 sys = 8.55 CPU) @ 11 +6959.06/s (n=1000000)
    The message is clear - a pretty precise 100% speed benefit from x. Of note is what happens internally:
    $ perl -MO=Deparse,-x7 -e'timethese(1000000, { map => sub { @_ = map 1 +, 1..5 }, x => sub { @_ = (1)x5 }})' timethese 1000000, {'map', sub { @_ = map(1, (1, 2, 3, 4, 5)); } , 'x', sub { @_ = (1) x 5; } }; -e syntax OK
    I'll spare you the corresponding output from 1..1000 - a list of 1000 numeric literals. In other words not only does the map take twice as long in every instance, it also consumes more memory - grossly so, for large lists.

    Makeshifts last the longest.