in reply to Re: How to split a string into chunks of length n or less
in thread How to split a string into chunks of length n or less
I found out that all versions are extremely fast, and thus not easy to benchmark. So probably my benchmark is flawed, take it with a grain of salt.
use strict; use warnings; use Benchmark qw(cmpthese); my $x = "0123456789abcdefg" x 6000 . "XXX"; sub subst { my $s = $x; $s =~ s/(.{16})/$1 /sg; $s }; sub up { join ' ', unpack '(A16)*', $x } sub m_substr { my $s = $x; for (my $i=16*int(length($s)/16);$i;$i-=16) { substr($s,$i,0)=" "; } $s; } sub readline { local $/ = \16; my $s; open my $handle, '<', \$x; $s = <$handle>; while (<$handle>) { $s .= ' ' . $_; } $s; } my $ref = subst(); sub wrap { my ($impl, $sub) = @_; die "Wrong result for $impl!" unless $sub->() eq $ref; } cmpthese(-1, { subst => wrap('subst', \&subst), unpack => wrap('unpack',\&up), substr => wrap('substr',\&m_substr), readl => wrap('readline', \&readline), })
Here are the results (with perl5.10.0):
And then I ran it again:Rate readl subst unpack substr readl 78783885/s -- -17% -18% -24% subst 95302528/s 21% -- -1% -8% unpack 96105165/s 22% 1% -- -8% substr 103929656/s 32% 9% 8% --
and thought "WTF?"Rate subst readl substr unpack subst 81695687/s -- -1% -36% -65% readl 82899185/s 1% -- -35% -65% substr 128382089/s 57% 55% -- -46% unpack 235800076/s 189% 184% 84% --
I increased the length of the source string by a factor of 100, and started the benchmark again. It still runs (after about 15 minutes).
My conclusions are that the differences are so small (or my benchmark so flawed...) that you can basically neglect them.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: How to split a string into chunks of length n or less
by Skeeve (Parson) on Aug 21, 2008 at 17:09 UTC |