#!/usr/bin/perl use strict; use warnings; use Benchmark qw( timethese cmpthese ) ; no if $] >= 5.018, warnings => "experimental::smartmatch"; my @array = ("world", "today", "is nice"); my %hash = ( 0 => "Hello", 1 => "world", 2 => "today", 3 => "is" , 4 => "nice" ); my @new_array; my $results = timethese(1000000000, { 'map' => benchmarkMap(), 'grep' => benchmarkGrep(), 'foreach' => benchmarkForeach(), 'smartmatch' => benchmarkSmartMatch(), }, 'none'); cmpthese( $results ) ; sub benchmarkSmartMatch { while (my ($key, $value) = each %hash) { push @new_array, $key if ($value ~~ @array); } } sub benchmarkGrep { while (my ($key, $value) = each %hash) { push @new_array, $key if (grep { /^$value$/ } @array); } } sub benchmarkForeach { while (my ($key, $value) = each %hash) { for (@array) {push @new_array, $key if ($value eq $_);} } } sub benchmarkMap { while (my ($key, $value) = each %hash) { map { if($value eq $_) { push @new_array, $key } } @array; } } __END__ Monks$ perl test.pl Rate foreach smartmatch grep map foreach 76569678/s -- -30% -59% -64% smartmatch 109170306/s 43% -- -42% -49% grep 187969925/s 145% 72% -- -13% map 215053763/s 181% 97% 14% --