use strict; use Benchmark qw/timethese/; my $str; my $index = 82; # construct a small string foreach (1..100) { $str .= "Element$_" . ' ' } timethese(100000, { 'FindNth_Roger' => '&FindNth_Roger', 'FindNth_Zaxo_Array' => '&FindNth_Zaxo_Array', 'FindNth_Zaxo_Split' => '&FindNth_Zaxo_Split', 'FindNth_Ysth' => '&FindNth_Ysth', 'FindNth_Pg' => '&FindNth_Pg', 'FindNth_Grantm' => '&FindNth_Grantm', 'FindNth_Jasper' => '&FindNth_Jasper', }); sub FindNth_Roger() { my $nth = @{[$str =~ m/\w+/g]}[$index-1]; } sub FindNth_Zaxo_Array() { my $nth = ($str =~ m/\w+/g)[$index-1]; } sub FindNth_Zaxo_Split() { my $nth = (split ' ', $str)[$index-1]; } sub FindNth_Ysth() { my $nth = [$str =~ m/\w+/g]->[$index-1]; } sub FindNth_Pg() { my ($nth) = ($str =~ m/(\w+\s*){$index}/); } sub FindNth_Grantm() { my $idx = $index - 1; my ($nth) = $str =~ /(?:\w+\W+){$idx}(\w+)/; } sub FindNth_Jasper() { my $nth; my %h; for ($str=~/.?/g) { $h{space} += !/\S/; $nth .= $_ if $h{space} == $index-1 && /\S/ .. !/\S/ } }