That is odd, because my benchmark indicates the opposite. Here is some benchmark code calling the two version with longer strings and more cuts each time. The results (shown at the end, slightly reformatted: perl 5.6.1) indicate a fairly predictable increase in efficiency of the unpack() method versus the substr() method:

#!/usr/bin/perl -w use strict; use Benchmark; my @args = ('ACGTACGTACGT', 2,4,5); timethese(-2,{ japhy => sub{japhy(@args)}, danger => sub{danger(@args)}, }); @args = ('ACGTACGTACGT' x 4, (2,4,5) x 4); timethese(-2,{ japhy => sub{japhy(@args)}, danger => sub{danger(@args)}, }); @args = ('ACGTACGTACGT' x 100, (2,4,5) x 100); timethese(-2,{ japhy => sub{japhy(@args)}, danger => sub{danger(@args)}, }); sub danger { my $s = shift; join '-', unpack join('A','',@_,'*') ,$s; } sub japhy { my $s = shift; join '-', map(substr($s, 0, $_, ''), @_),$s; } __END__ Benchmark: running danger, japhy, each for at least 2 CPU seconds... danger: 1 wallclock secs ( 2.06 usr + 0.00 sys = 2.06 CPU) @ 10438.35/s (n=21503) japhy: 1 wallclock secs ( 2.06 usr + 0.00 sys = 2.06 CPU) @ 8694.17/s (n=17910) Benchmark: running danger, japhy, each for at least 2 CPU seconds... danger: 2 wallclock secs ( 2.03 usr + 0.00 sys = 2.03 CPU) @ 4800.49/s (n=9745) japhy: 3 wallclock secs ( 2.10 usr + 0.00 sys = 2.10 CPU) @ 3409.05/s (n=7159) Benchmark: running danger, japhy, each for at least 2 CPU seconds... danger: 2 wallclock secs ( 2.13 usr + 0.00 sys = 2.13 CPU) @ 258.22/s (n=550) japhy: 3 wallclock secs ( 2.01 usr + 0.00 sys = 2.01 CPU) @ 155.72/s (n=313)

Of course, it is entirely possible that I've completely messed up the benchmark.


In reply to Re: Re: Re: Adding gaps to a sequence by danger
in thread Adding gaps to a sequence by ariels

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.