use Benchmark qw(cmpthese); sub substrings_TheHobbit { my $string = shift; my @result = (); foreach my $length (1..length($string)) { foreach my $offset (0..length($string)-$length) { push @result,substr($string,$offset,$length); } } return @result; } sub substrings_thelenm { my $string = shift; my @result = (); my $strlen = length $string; foreach my $length (1..$strlen) { foreach my $offset (0..$strlen-$length) { push @result, substr($string,$offset,$length); } } return @result; } cmpthese(-10, { TheHobbit => sub { substrings_TheHobbit("Just Another Perl Hacker,") }, thelenm => sub { substrings_thelenm("Just Another Perl Hacker,") }, }); __output__ Benchmark: running TheHobbit, thelenm, each for at least 10 CPU seconds... TheHobbit: 12 wallclock secs (10.51 usr + 0.03 sys = 10.54 CPU) @ 804.55/s (n=8480) thelenm: 14 wallclock secs (10.44 usr + 0.02 sys = 10.46 CPU) @ 809.37/s (n=8466) Rate TheHobbit thelenm TheHobbit 805/s -- -1% thelenm 809/s 1% --