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.

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% + --

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller



In reply to Re: Optimizing the bejeezus out of a sub by BrowserUk
in thread Optimizing the bejeezus out of a sub by sgifford

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.