Thanks for reply.
i did some benchmarking and found that map is slower than foreach. Also I was under impression that map is actually faster than foreach, but my result is different !
#!/opt/gsperl-5.8.6_1/bin/perl
use strict;
use Benchmark;
my ($start,$end,$diff);
my @data = (1..20000000);
# start timer
$start = new Benchmark;
my %dataseen;
my @arr;
foreach my $x (@data) {
push @arr, ($x+2);
}
# end timer
$end = new Benchmark;
# calculate difference
my $diff = timediff($end, $start);
# report
print "Time taken by foreach loop was ", timestr($diff, 'all'), " seco
+nds\n";
my $start1 = new Benchmark;
my @arr1;
@arr1 = map { push @arr1, ($_+ 2) } @data;
# end timer
my $end1 = new Benchmark;
# calculate difference
my $diff1 = timediff($end1, $start1);
# report
print "Time taken by map block was ", timestr($diff1, 'all'), " second
+s\n";
and output of code :
Time taken by foreach loop was 9 wallclock secs ( 8.22 usr 0.32 sys + 0.00 cusr 0.00 csys = 8.54 CPU) seconds
Time taken by map block was 15 wallclock secs (14.88 usr 0.72 sys + 0.00 cusr 0.00 csys = 15.60 CPU) seconds
|