I'm using Text::Balanced for an implementation of template like system. I've gotten it to work exactly as it's supposed to, except it's horribly slow. I'm wondering if I'm doing something wrong. The idea is that given some text marked up like so:
This is text 0. <SECTION[1]> This is text 1. </SECTION> This is text 0. <SECTION[2]> This is text 2. </SECTION>
We specify which section tags to show and which to delete. So, if I called this function on the above text with only section 1 set to show, I'd get:
This is text 0. This is text 1. This is text 0.
Here's my implementation/test program:
#!/usr/bin/perl -d:DProf use Carp; use Text::Balanced; our $extract_section_tag = Text::Balanced::gen_extract_tagged( '<SEC +TION\[([^\]]+)\]>\n?', '</SECTION>\n?', '[\S\s]*?(?=<SECTION\[)' ); my $page; $page .= "This is some filler.\n" x 20000; $page .= "<SECTION[test1]>\n"; $page .= "This is some filler.\n" x 20000; $page .= "</SECTION>\n"; $page .= "This is some filler.\n" x 20000; $page .= "<SECTION[test2]>\n"; $page .= "This is some filler.\n" x 20000; $page .= "</SECTION>\n"; $page .= "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 ($tag_section, $post, $pre, $tag_open, $content, $tag_close, @i +nfo); my $return = ''; while ( @info = $extract_section_tag->($page) ) { ($tag_section, $post, $pre, $tag_open, $content, $tag_close) = + @info; if (! (defined $tag_section && length $tag_section) ) { if ($post =~ m/<SECTION\[/) { my $excerpt = substr($post, 0, 100); print STDERR "\n"; Carp::carp("Warning: Unbalanced SECTION tags. Fix the + template! Error near: $excerpt\n"); } last; } my $show = 0; if ($tag_open =~ m/<SECTION\[(.*?)\]>/) { $show = 1 if (exists $hashref->{$1}); } if ($show && $content =~ m/<SECTION/) { $content = process_section($content, $hashref); } $return .= $pre . ($show ? $content : ''); $page = $post; } $return .= $post; return $return; }
The above code takes over 2 seconds to run on my system, and it gets even worse as the page it's processing gets larger. How can I improve the speed of this? I'm using Text::Balanced v2.0.0, which fixes the issue of a $& variable slowing down all regexps.

In reply to 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.