in reply to Optimizing the bejeezus out of a sub
The only thing left inside the routine that might be trimable is the inner foreach loop. If the list of aliases generated by @$sec is of any size, then you maybe paying a time penalty for building that list, and might realise some saving by using indexes to iterate over the array
for (0..$#{$sec}) { $sec->[$_][_type]... }
Also, if _type, _sec & _contents aren't already defined as constants (which are usually UPPERCASE by convention) then making them constants may yield another minor improvement.
However, given that each call to the routine is only taking an average of 0.000165 seconds, there isn't exactly a lot of fat to be trimed within the routine. The fact that you are spending 50% of your programs time, a little over 20 seconds in there comes down to the fact that you are calling it 130,000 times.
There are a few possibilities that might improve things.
$self->[_output] .= ... and then calling print once at the end of the (external) loop that is calling this 130,000 times.
If there is no possibility of reducing the 130,000 calls to the routine, then you might save some by not calling the routine! That is to say, if you inlined the sub at the point(s) of call, you would save the cost of building the stackframe and calling it.
Whether this is
Are both questions you must decide given your requirements. How much benefit is realisable will depend on the nature of the code that calls this routine.
As an attempt to give some indication, the following (unrepresentative and possibly misrepresentative) benchmark shows that under some circumstances, inlining and/or deferring the printing, can yield some possibly significant benefits.
#! perl -slw use strict; use Benchmark qw[cmpthese]; sub printto { print {$_[0]} $_[1]; return; } sub defer { ${$_[0]} .= $_[1].$/; return; } open our $fh, '>', 'junk.out' or die $!; cmpthese( -60, { direct=> q[ print $fh 'Direct'; for(0..130_000) { printto $fh, $_; } ], inlined=> q[ print $fh 'Inlined'; for(0..130_000) { print $fh $_; } ], deferred=> q[ print $fh 'Deferred'; my $output = ''; for(0..130_000) { defer \$output, $_ } print $fh $output; ], inline_deferred=> q[ print $fh 'Inline-deffered'; my $output=''; for(0..130_000) { $output .= $_ . $/; } print $fh $output; ], }); close $fh; __END__ D:\Perl\test>test s/iter direct inlined deferred inlin +e_deferred direct 7.57 -- -48% -57% + -85% inlined 3.93 93% -- -17% + -71% deferred 3.26 132% 20% -- + -65% inline_deferred 1.13 568% 247% 188% + --
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Optimizing the bejeezus out of a sub
by diotalevi (Canon) on Jun 24, 2003 at 13:22 UTC | |
by BrowserUk (Patriarch) on Jun 24, 2003 at 13:27 UTC | |
by diotalevi (Canon) on Jun 24, 2003 at 13:32 UTC | |
by BrowserUk (Patriarch) on Jun 24, 2003 at 13:42 UTC | |
by diotalevi (Canon) on Jun 24, 2003 at 13:57 UTC | |
|