Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'm looking for some places I can squeeze a little bit more performance out of a sub. dprof says this sub is running 50% of the time for my application, so it looks like my best candidate for optimization.
# Print the given template sections to the supplied filehandle sub printto { my $self = shift; my $fh = shift; my $ret = 0; my($val,$sec,$v); for(;@_;shift) { $sec = $self->[_sec]{$_[0]}; if (!defined($sec)) { print $fh $self->_nosuchsec($_[0]); next; } $ret++; foreach $v (@$sec) { if ($v->[_type] == type_text) { print $fh $v->[_contents]; } else { $val = $self->[_assign]{$v->[_contents]}; print $fh defined($val) ? $val : $self->_nosuchvar($val,"\$".$val); } } } $ret; }
This is the output routine for a templating engine. It takes as parameters an object, a filehandle, and zero or more template section names to display. For each template section name, if the section exists it displays all parts of it. A part is either plain text, or else a variable; variables are looked up in the $self->[_assign] hashref. The _nosuchsec and _nosuchvar routines are error handlers, and are never called in normal operation.

Here's what DProf says:

%Time ExclSec CumulS #Calls sec/call Csec/c  Name
 51.9   21.53 21.280 130000   0.0002 0.0002  FTS::printto

Any ideas where to look for that last little bit of performance?

Thanks!


In reply to 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-24 02:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found