#!/usr/bin/perl# https://wimvanderbauwhede.github.io/articles/writing-faster-perl/# The foreach-loop version takes 2.6 s, the map version 3.3 s, so the map is# 25% slower. For reference, the index-based for-loop version takes 3.8 s and# the C-style for-loop version 4.4 s — don't do that! usestrict;usewarnings;useutf8;usefeatureqw/say/;useBenchmarkqw/:hireswallclock :all/;subtest_foreach{my($things)=@_;my@res;foreach(@$things){push@res,2*$_+1;}return\@res;}subtest_for_idx{my($things)=@_;my@res;my$number_of_items=scalar(@$things);for(my$i=0;$i<$number_of_items;++$i){$res[$i]=$things->[$i]*2+1}return\@res;}subtest_map{my($things)=@_;return[map{2*$_+1}@$things];}subthings{my($number_of_items)=@_;return[map{$_}(1..$number_of_items)];}subexperiment{my($count,$number_of_items)=@_;say"#listsize=${number_of_items}";clearallcache();my$r=timethese($count,{"foreach"=>sub{my$x=test_foreach(things($number_of_items))},"for-index"=>sub{my$x=test_for_idx(things($number_of_items))},"map"=>sub{my$x=test_map(things($number_of_items))},});cmpthese($r);}experiment(10_000,$_)for(3,100,1_000,10_000);