Is there any reason why a function call would be quicker accessing a lexical variable?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 Hacke +r,") }, thelenm => sub { substrings_thelenm("Just Another Perl Hacker, +") }, }); __output__ Benchmark: running TheHobbit, thelenm, each for at least 10 CPU second +s... TheHobbit: 12 wallclock secs (10.51 usr + 0.03 sys = 10.54 CPU) @ 80 +4.55/s (n=8480) thelenm: 14 wallclock secs (10.44 usr + 0.02 sys = 10.46 CPU) @ 80 +9.37/s (n=8466) Rate TheHobbit thelenm TheHobbit 805/s -- -1% thelenm 809/s 1% --
In reply to Re: Re: Finding all substrings
by broquaint
in thread Finding all substrings
by TheHobbit
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |