Well, I get it to almost the same speed as the double reverse,
but only after some modification:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw /cmpthese/;
chomp (our @strings = <DATA>);
our (@a, @b, @c, @d);
cmpthese -1 => {
juerd => '@a = map {scalar reverse ucfirst lc reverse} @strings',
davido => '@b = map {my $t = $_; $t =~ s/^(.*)(.)$/\L$1\U$2/; $t}
+@strings',
dada1 => '@c = map {lc (substr ($_, 0, length ($string) - 1)) .
uc (substr ($_, -1, 1))} @strings',
dada2 => '@d = map {lc (substr ($_, 0, -1)) .
uc (substr ($_, -1, 1))} @strings',
};
die 1 unless "@a" eq "@b";
die 2 unless "@a" eq "@c";
die 3 unless "@a" eq "@d";
__DATA__
ACta
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnop
+qrstuvwxyz
I have a question!
yet another way, which should be quickier than the double reverse:
Rate davido dada1 dada2 juerd
davido 21081/s -- -68% -70% -70%
dada1 65511/s 211% -- -6% -7%
dada2 69592/s 230% 6% -- -1%
juerd 70275/s 233% 7% 1% --
Abigail
-
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.