In a Perl.com article on bioinformatics and Perl, there was an interesting snippet used as a demonstration for different optimization techniques. Now, the optimization technique described was to inline code, if it was small. I'm not disputing that.
while ((my $begin, my $end) = each %exon_endpoints) { print get_exon($chromosome, $begin, $end), "\n\n"; } sub get_exon { my($chromosome, $begin, $end) = @_; # The arguments to substr are: string, beginning, length return substr($chromosome, $begin - 1, $end - $begin + 1); }

becomes ...

while ((my $begin, my $end) = each %exon_endpoints) { print substr($chromosome, $begin - 1, $end - $begin + 1), "\n\n"; }

Now, it's relatively obvious that the first option is the more readable, and the author says so, as well. The second, given a large enough $chromosome, is much faster.

I was wondering if there were other optimizations than passing by reference (as suggested in an earlier optimization option). The only one I could think of was to use closures, along the lines of:

my $chromosome; get_chromosome(1, \$chromosome); my $get_exon = sub { substr $chromosome, $_[0] - 1, $_[1] - $_[0] - 1; }; while (my @ends = each %exon_endpoints) { print $get_exon->(@ends), "\n\n"; }

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.


In reply to Large data processing ... by dragonchild

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.