I can't provide a precise answer, but that recursion passing $page by value is probably a good place to start looking. But more importantly, if in fact your task is a simple as stated, why not just use a progressive regex ? Here's my quick hack at it, and it runs quite quickly (Note I haven't actually validated the output string, other than noting the length change reported at exit):
use Carp; use strict; use warnings; my $page = join('', "This is some filler.\n" x 20000, "<SECTION[test1]>\n", "This is some filler.\n" x 20000, "</SECTION>\n", "This is some filler.\n" x 20000, "<SECTION[test2]>\n", "This is some filler.\n" x 20000, "</SECTION>\n", "This is some filler.\n" x 20000); print "Calling process_section. Length of page=[" . length($page) ."] +\n"; my $newpage = process_section(\$page, {test1 => 1}); print "Done. Length of newpage=[" . length($newpage) ."]\n"; sub process_section { my ($page, $hashref) = @_; my $return = ''; while ($$page=~/\G(.*?)<SECTION\[([^\]]+)\]>\n?/igcs) { $return .= $1; my $tag = $2; my $post = pos($$page); if ($$page=~/\G(.*?)<\/SECTION>\n?/igcs) { $return .= $1 if exists $hashref->{$tag}; next; } my $excerpt = substr($$page, $post, 100); print STDERR "\n"; Carp::carp("Warning: Unbalanced SECTION tags. Fix the templat +e! Error near: $excerpt\n"); pos($$page) = $post; } $return .= substr($$page, pos($$page)) if (pos($$page) < length($$page)); return $return; }
Note that I've changed to use a ref to the $page, and eliminated recursion.

Text::Balanced is a great tool for dealing with complex things like parsing Perl code, but if you can get by with a good old RE, I'd say its probably a much better solution.


Perl Contrarian & SQL fanboy

In reply to Re: Am I using Text::Balanced correctly? Speed issues. by renodino
in thread Am I using Text::Balanced correctly? Speed issues. by solri1

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.