#! /usr/bin/perl use strict; use warnings; use feature 'say'; # use Benchmark qw(:all) ; # WindowsOS use Benchmark::Forking qw( timethese cmpthese ); # UnixOS sub for_loop { for my $iloop (0 .. 10){ say $iloop; } return; } sub while_loop { my @numbers = qw( 1 2 3 4 5 6 7 8 9 10 ); while (defined(my $int = shift @numbers)) { say $int; } return; } my $results = timethese(1000000, { While => \&while_loop, ForEach => \&for_loop, }, 'none'); cmpthese( $results ); __END__ $ perl test.pl s/iter While ForEach While 2.32 -- -14% ForEach 1.98 17% --