On Wed, Jun 1, 2011 at 19:02, Ronald J Kimball <r...@tamias.net> wrote: > On Wed, Jun 01, 2011 at 02:36:40PM +0100, Paul Makepeace wrote: >> On Tue, May 31, 2011 at 22:41, Nick Patch <n...@atemoya.net> wrote: >> > use Benchmark qw( cmpthese ); >> > >> > cmpthese(-2, { >> > assign => sub { my $x = 'bar'; $x = "foo/$x" }, >> > substr => sub { my $x = 'bar'; substr $x, 0, 0, 'foo/' }, >> > lvalue => sub { my $x = 'bar'; substr($x, 0, 0) = 'foo/' }, >> > }); >> >> Oddly(?) join() is 20% faster than assignment: >> >> ... >> join => sub { my $x = 'bar'; $x = join('/', 'foo', 'bar') }, >> oooscf => sub { my $x = 'bar'; $x = File::Spec->catfile($x, 'bar') }, >> fnoscf => sub { my $x = 'bar'; $x = catfile($x, 'bar') }, > > You're not benchmarking the right behavior. In particular, your join sub > is not using $x in the call to join. > > join => sub { my $x = 'bar'; $x = join('/', 'foo', $x) }, > oooscf => sub { my $x = 'bar'; $x = File::Spec->catfile('foo', $x) }, > fnoscf => sub { my $x = 'bar'; $x = catfile('foo', $x) },
Yes, sorry, code paste fail. The results paste was right though :) As penance, here's an exploration of O(n) versus, er, not... Do the benchmarks but with a repetition which mirrors how larger output in general is built. *join is the only one that's linear*, and substr, while the winner on smaller n, takes a severe beating as the string length goes up. The naive assignment sucks but not as horribly. #!/usr/bin/perl use Benchmark qw( cmpthese ); use Devel::Leak; use File::Spec; use File::Spec::Functions; sub concat_bm { my $count = shift; cmpthese(-2, { assign => sub { my $x = 'bar'; for (1..$count) { $x = "foo/$x" } }, substr => sub { my $x = 'bar'; for (1..$count) { substr $x, 0, 0, 'foo/' } }, lvalue => sub { my $x = 'bar'; for (1..$count) { substr($x, 0, 0) = 'foo/' } }, join => sub { my $x = 'bar'; my @foos = ('foo') x $count; { $x = join('/', @foos, $x) } }, }); } my $count = Devel::Leak::NoteSV($handle); concat_bm(10); concat_bm(100); concat_bm(1000); concat_bm(10000); Devel::Leak::CheckSV($handle); Rate lvalue assign join substr lvalue 311229/s -- -3% -32% -41% assign 321920/s 3% -- -30% -39% join 458335/s 47% 42% -- -13% substr 526317/s 69% 63% 15% -- Rate lvalue assign substr join lvalue 26302/s -- -11% -30% -55% assign 29466/s 12% -- -22% -49% substr 37574/s 43% 28% -- -35% join 57931/s 120% 97% 54% -- Rate lvalue substr assign join lvalue 660/s -- -7% -56% -89% substr 710/s 8% -- -52% -88% assign 1490/s 126% 110% -- -75% join 6002/s 809% 746% 303% -- Rate lvalue substr assign join lvalue 7.73/s -- -0% -67% -99% substr 7.73/s 0% -- -67% -99% assign 23.3/s 202% 202% -- -96% join 570/s 7270% 7270% 2341% -- Paul _______________________________________________ Boston-pm mailing list Boston-pm@mail.pm.org http://mail.pm.org/mailman/listinfo/boston-pm