Right you are ;)
use warnings; use strict; use Benchmark qw(cmpthese); my $str = "Just Another Perl Hacker"; print join " ", do_substr (), "\n"; print join " ", do_regex (), "\n"; print join " ", do_unpack (), "\n"; print "\n"; cmpthese (-1, { unpack => \&do_unpack, substr => \&do_substr, regex => \&do_regex, } ); sub do_substr { my @parts; push @parts, substr $str, $_, 3 for 0 .. length ($str) - 3; return @parts; } sub do_regex { my @parts = $str =~ /(?=(...))/g; return @parts; } sub do_unpack { my $matches = length ($str) - 2; my $temp = "a3XX" x $matches; my @parts = unpack ($temp, $str); return @parts; }
Prints:
Jus ust st t A An Ano not oth the her er r P Pe Per erl rl l H H +a Hac ack cke ker Jus ust st t A An Ano not oth the her er r P Pe Per erl rl l H H +a Hac ack cke ker Jus ust st t A An Ano not oth the her er r P Pe Per erl rl l H H +a Hac ack cke ker Rate regex unpack substr regex 42045/s -- -12% -39% unpack 47764/s 14% -- -31% substr 69161/s 64% 45% --
Update: added unpack
In reply to Re^2: finding number of contiguous letters
by GrandFather
in thread finding number of contiguous letters
by Marknel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |