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.# 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; }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |