Just fyi:
use strict;
use warnings;
use Tie::CharArray;
use Benchmark qw/cmpthese/;
my $string = join '', 'A' .. 'Y';
sub _unpack {
my @arr = unpack '(A5)*', $string;
}
sub _regex {
my @arr = $string =~ /.{5}/g;
}
sub _split {
my @arr = split /.{5}\K/, $string;
}
sub _substr {
my @arr;
for ( my $i = 0 ; $i < length $string ; $i += 5 ) {
push @arr, substr $string, $i, 5;
}
}
sub _open {
my @arr;
open my $sh, '<', \$string;
while ( read $sh, my $chars, 5 ) {
push @arr, $chars;
}
}
cmpthese(
-5,
{
_unpack => sub { _unpack() },
_regex => sub { _regex() },
_split => sub { _split() },
_substr => sub { _substr() },
_open => sub { _open() }
}
);
Output:
Rate _open _regex _substr _split _unpack
_open 265986/s -- -53% -55% -57% -70%
_regex 563780/s 112% -- -5% -8% -36%
_substr 593788/s 123% 5% -- -3% -33%
_split 612001/s 130% 9% 3% -- -31%
_unpack 881949/s 232% 56% 49% 44% --
-
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.