in reply to Re: finding number of contiguous letters
in thread finding number of contiguous letters
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: finding number of contiguous letters
by Skeeve (Parson) on May 23, 2007 at 12:31 UTC | |
|
Re^3: finding number of contiguous letters
by Limbic~Region (Chancellor) on May 23, 2007 at 12:44 UTC | |
by GrandFather (Saint) on May 23, 2007 at 21:01 UTC | |
by Limbic~Region (Chancellor) on May 24, 2007 at 12:39 UTC | |
by GrandFather (Saint) on May 24, 2007 at 21:47 UTC |