| Paul Lalli 2006-01-10, 4:02 am |
| Adriano Ferreira wrote:
> On 1/6/06, Sai Tong <stong@fidelio.rutgers.edu> wrote:
>
>
> Maybe you can use C<map>
>
> my @return_values = map { $_->method } @array_of_objects;
>
> But that's not going to be fast (as well as the for construction) if
> the repeated calls of C<$_->method> aren't fast enough (for your
> purposes).
I find it unlikely map is going to provide any significant speed-ups
over push:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw/cmpthese/;
my @original = (1..1_000);
cmpthese (10_000, {
'push' => sub {
my @results;
for (@original){
push @results, $_ * 2;
}
},
'map' => sub {
my @results = map { $_ * 2 } @original;
},
}
);
__END__
Benchmark: timing 10000 iterations of map, push...
map: 42 wallclock secs (41.72 usr + 0.00 sys = 41.72 CPU) @
239.69/s (n=10000)
push: 44 wallclock secs (43.24 usr + 0.00 sys = 43.24 CPU) @
231.27/s (n=10000)
Rate push map
push 231/s -- -4%
map 240/s 4% --
Paul Lalli
|