Warning: objections have been raised to the following code - see below for the objections and the revised code

Hmm, I think I own you now - this uses code that writes code:

[%# Run this through tpage - from Template %] [% numbers = [ 1, 2, 3, 4, 5, 6 ] -%] [% end = '\)' -%] [% start = '\w+\(' -%] my %functions = (); [% FOR level = numbers -%] $functions{ [% level %] } = sub { $_[0] =~ s/[% end %]{[% level %]}$//o; $_[0] =~ s/^([% start %]){[% level %]}//o; return $_[0]; }; [% END -%] sub get_proparg_evdb { $_ = $_[0]; my $level = $_[1] || return $_; if ($level == -1) { m/\(([^()]+)\)+/; return $1; } return &{$functions{ $level }}( $_ ) || die 'Add universal function here'; }
This code, when parsed by tpage which comes with Template will create a special function for each of the cases with an optimised regexp in it. These functions are then accessed through a hash.

The complete code that is produced is here:

my %functions = (); $functions{ 1 } = sub { $_[0] =~ s/\){1}$//o; $_[0] =~ s/^(\w+\(){1}//o; return $_[0]; }; $functions{ 2 } = sub { $_[0] =~ s/\){2}$//o; $_[0] =~ s/^(\w+\(){2}//o; return $_[0]; }; $functions{ 3 } = sub { $_[0] =~ s/\){3}$//o; $_[0] =~ s/^(\w+\(){3}//o; return $_[0]; }; $functions{ 4 } = sub { $_[0] =~ s/\){4}$//o; $_[0] =~ s/^(\w+\(){4}//o; return $_[0]; }; $functions{ 5 } = sub { $_[0] =~ s/\){5}$//o; $_[0] =~ s/^(\w+\(){5}//o; return $_[0]; }; $functions{ 6 } = sub { $_[0] =~ s/\){6}$//o; $_[0] =~ s/^(\w+\(){6}//o; return $_[0]; }; sub get_proparg_evdb { $_ = $_[0]; my $level = $_[1] || return $_; if ($level == -1) { m/\(([^()]+)\)+/; return $1; } return &{$functions{ $level }}( $_ ) || die 'Add universal function here'; }
By adding this to the benchmaring stuff above from previous posts using require evdb_functions.pl (or whatever) and Benchmarking you get this (some suggestions omitted for clarity):
Rate ori m-elegant ori++ evdb ori 6234/s -- -36% -40% -51% m-elegant 9690/s 55% -- -7% -24% ori++ 10373/s 66% 7% -- -18% evdb 12690/s 104% 31% 22% --

Code that writes code - tip 29 from the pragmatic programmer.

--tidiness is the memory loss of environmental mnemonics


In reply to Re: Re: Re: Re: Peeling the Peelings by EvdB
in thread Peeling the Peelings by PetaMem

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.