in reply to Re: Re: array pre-allocation trick
in thread array pre-allocation trick

The map in your example serves no purpose other than to slow it down. The (0 .. $::size-1) builds a list which is directly assignable to an array.

By adding the map in there, you are

  1. building a list
  2. breaking the list down to feed it to the map element by element
  3. the reassembling a new (identical) list before assigning it to the array.

Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy

Replies are listed 'Best First'.
Re: Re: Re: Re: array pre-allocation trick
by Anonymous Monk on Nov 02, 2002 at 00:24 UTC
    True the map doesn't do much, but it's a minimal benchmark for Aristotle's suggestion:
    my @array = map { do_stuff_with($_); $_ } 1 .. 5000;
    I've replaced "do_stuff_with($_)" with a no-op. It didn't perform as well as the others, so that leads me to think I shouldn't pursue using map in this case.
      That very much depends on your setup. map can be the faster, slower or equal variant. There's too many constraints that play a role - you can't just say "this is faster" based on a minimal benchmark because the characteristics of each looping approach lend themselves to different data structure and control flow designs. If you take a forish approach, using map instead will be awkard - but the same is true in the reverse case.

      Makeshifts last the longest.