in reply to Re: Finding all substrings
in thread Finding all substrings

Actually it makes it ever so slightly slower, much to my surprise :-/
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% --
Is there any reason why a function call would be quicker accessing a lexical variable?
_________
broquaint

Replies are listed 'Best First'.
Re: Re: Re: Finding all substrings
by samtregar (Abbot) on Apr 24, 2002 at 18:26 UTC
    I bet it's because length() is special. All SVs cache their length already, so putting it in a lexical is just a slower way to cache length().

    -sam

Re: Re: Re: Finding all substrings
by erikharrison (Deacon) on Apr 24, 2002 at 18:29 UTC

    Some repeated benchmarking answers this . . .

    my is expensive. Removing the my and making $strlen a global eliminates the diferrence for the small test case. The bench about the same. Multiplying the length of the test string by ten and leaving $strlen global gives thelenm's sub big gains over the orginal - %6-%10. Finally, making $strlen lexical and retesting reduces those gains by %4-%8, still testing with "Just Another Perl Hacker,"x10.

    So, yes, this is definately an optimization.

    Cheers,
    Erik