No, and for good reason - its very slow compared to a normal solution. That "e" stands for "eval string", which is pretty sluggish, and should only be used for good reason. Compare:
use Benchmark;
use strict;
our $dummy = 0;
our %count;
my $gen = "";
foreach my $l (qw|a t c g|) {
$gen .= "$l$_" for qw|a t c g|;
}
my @gen = split '', $gen;
my %tests;
foreach my $test ( qw|chain subst hash_array hash_string| ) {
no strict refs;
$tests{$test} = sub { *{$test}{CODE}->($gen,\@gen) }
}
timethese(100000, \%tests);
sub chain {
my ($gen,$genome) = @_;
for my $i (0..$#$genome) {
if (($genome->[$i] eq 'a') && ($genome->[$i+1] eq 'a')) { +
++$dummy; }
elsif (($genome->[$i] eq 'a') && ($genome->[$i+1] eq 'g')) { +
++$dummy; }
elsif (($genome->[$i] eq 'a') && ($genome->[$i+1] eq 'c')) { +
++$dummy; }
elsif (($genome->[$i] eq 'a') && ($genome->[$i+1] eq 't')) { +
++$dummy; }
elsif (($genome->[$i] eq 't') && ($genome->[$i+1] eq 'a')) { +
++$dummy; }
elsif (($genome->[$i] eq 't') && ($genome->[$i+1] eq 'g')) { +
++$dummy; }
elsif (($genome->[$i] eq 't') && ($genome->[$i+1] eq 'c')) { +
++$dummy; }
elsif (($genome->[$i] eq 't') && ($genome->[$i+1] eq 't')) { +
++$dummy; }
elsif (($genome->[$i] eq 'c') && ($genome->[$i+1] eq 'a')) { +
++$dummy; }
elsif (($genome->[$i] eq 'c') && ($genome->[$i+1] eq 'g')) { +
++$dummy; }
elsif (($genome->[$i] eq 'c') && ($genome->[$i+1] eq 'c')) { +
++$dummy; }
elsif (($genome->[$i] eq 'c') && ($genome->[$i+1] eq 't')) { +
++$dummy; }
elsif (($genome->[$i] eq 'g') && ($genome->[$i+1] eq 'a')) { +
++$dummy; }
elsif (($genome->[$i] eq 'g') && ($genome->[$i+1] eq 'g')) { +
++$dummy; }
elsif (($genome->[$i] eq 'g') && ($genome->[$i+1] eq 'c')) { +
++$dummy; }
elsif (($genome->[$i] eq 'g') && ($genome->[$i+1] eq 't')) { +
++$dummy; }
}
}
sub subst {
my ($gen,$genome) = @_;
$gen=~s/(.)(?=(.))/++$count{$1.$2} and undef/eg;
}
sub hash_array {
my ($gen,$genome) = @_;
$count { $genome -> [$_] . $genome -> [$_ + 1] } ++ for (0..$#$gen
+ome);
}
sub hash_string {
my ($gen,$genome) = @_;
$count { substr($gen, $_, 2) }++ for (0..length($gen)-2);
}
__DATA__
Benchmark: timing 100000 iterations of chain, hash_array, hash_string,
+ subst...
chain: 64 wallclock secs (41.09 usr + 0.00 sys = 41.09 CPU) @ 2
+433.68/s (n=100000)
hash_array: 14 wallclock secs (11.24 usr + 0.00 sys = 11.24 CPU) @ 8
+896.80/s (n=100000)
hash_string: 8 wallclock secs ( 6.26 usr + 0.00 sys = 6.26 CPU) @ 1
+5974.44/s (n=100000)
subst: 17 wallclock secs (16.60 usr + 0.00 sys = 16.60 CPU) @ 6
+024.10/s (n=100000)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|