in reply to Re: Adding gaps to a sequence
in thread Adding gaps to a sequence

My tests proved otherwise -- else, I would have used that code in my reply. Since it wasn't faster, I looked for some other avenue.

Jeff japhy Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-Q)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Re: Re: Adding gaps to a sequence
by danger (Priest) on Jul 15, 2001 at 22:38 UTC

    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 fairness, I should add that my original version is the slowest, by a fair margin (and gets much worse as the string gets longer). Adding `gapify' in danger's benchmark yields (with Perl 5.005_03):
      Benchmark: running danger, gapify, japhy, each for at least 2 CPU seco +nds... danger: 3 wallclock secs ( 2.00 usr + 0.00 sys = 2.00 CPU) @ 66 +271.00/s (n=132542) gapify: 2 wallclock secs ( 2.37 usr + 0.00 sys = 2.37 CPU) @ 41 +536.06/s (n=98302) japhy: 2 wallclock secs ( 2.55 usr + 0.00 sys = 2.55 CPU) @ 55 +071.76/s (n=140433) Benchmark: running danger, gapify, japhy, each for at least 2 CPU seco +nds... danger: 3 wallclock secs ( 1.98 usr + 0.02 sys = 2.00 CPU) @ 31 +026.00/s (n=62052) gapify: 2 wallclock secs ( 2.07 usr + 0.00 sys = 2.07 CPU) @ 14 +864.03/s (n=30719) japhy: 2 wallclock secs ( 2.00 usr + 0.00 sys = 2.00 CPU) @ 23 +104.50/s (n=46209) Benchmark: running danger, gapify, japhy, each for at least 2 CPU seco +nds... danger: 2 wallclock secs ( 2.00 usr + 0.00 sys = 2.00 CPU) @ 19 +81.00/s (n=3962) gapify: 2 wallclock secs ( 2.03 usr + 0.00 sys = 2.03 CPU) @ 18 +7.87/s (n=382) japhy: 2 wallclock secs ( 2.08 usr + 0.00 sys = 2.08 CPU) @ 12 +27.84/s (n=2558)
      No, it's my fault. I had constructed a slower function:
      sub japhy { my $s = shift; join '-', unpack join('', map "A$_", @_), $s; }


      _____________________________________________________
      Jeff japhy Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-Q)}y js++=;shajsj<++y(p-q)}?print:??;