#!/usr/bin/perl # # Is map really faster then foreach? # use Benchmark qw( cmpthese timethese); use warnings; use strict; my @array = `cat /usr/share/dict/linux.words`; sub for_loop { my $i; for (@array){ $i++; } } sub foreach_loop { my $i; foreach my $line (@array){ $i++; } } sub map_loop { my $i; map { $i++ } @array; } timethese (-30, { # number indicates seconds to run the test for (-5 is 5seconds) or total test runs to do (if number +) 'foreach method' => sub { foreach_loop() }, 'for method' => sub { for_loop() }, 'map method' => sub { map_loop() }, });