On Tue, Aug 3, 2010 at 09:47, Chas. Owens <chas.ow...@gmail.com> wrote: > On Tue, Aug 3, 2010 at 08:44, Shawn H Corey <shawnhco...@gmail.com> wrote: >> On 10-08-03 06:43 AM, Rob Coops wrote: >>> >>> Third you could of course when you are printing the values from the array >>> add the linefeeds: >>> print join("\n", @myarray); >>> or >>> foreach my $value ( @myarray ) { >>> print $value . "\n"; >> >> When printing, use a list; it's faster. >> >> print $value, "\n"; > snip > > I hate it when some makes a blanket statement of "it's faster" without > providing a benchmark or a reason. In the simple case, the comma is > slower than the period and interpolation is slower still.
Whoops, accidentally sent too early. My point was that even though concatenation is faster than pass a list or an interpolated string (contrary to the claim made earlier), it isn't really that much faster. Especially once you factor in the cost of writing to disk (which the benchmark avoids to prevent noise). Now, you might make the argument that the comma is clearer than the concatenation, but that point is debatable. For one thing, these two statements are not identical: print "foo", "\n"; print "foo" . "\n"; The first will be affected by the value of $, and the second won't. The second will also benefit from compile time constant folding: perl -MO=Deparse -e 'print "foo" . "\n"' print "foo\n"; perl -MO=Deparse -e 'print "foo", "\n"' print 'foo', "\n"; Personally, I use string interpolation even though it is the slowest of the three methods. I find string interpolation to be superior to passing a list because it is not affected by $, and more readable than having tiny periods floating around. It also requires fewer characters on average (you usually have at least one set of double quotes in the expression already). To sum up: if you are looking for something like this to speed up your program than you are well and truly hosed. Choose a method that looks right to your team and has the right behavior. Profile your code to find spots were you are slow and optimize those spots by changing the algorithm or moving code down a level to C (via XS or Inline::C), not by relying on folk-wisdom micro-optimizations. One item: Rate string comma period string 3588672/s -- -3% -11% comma 3714590/s 4% -- -7% period 4014817/s 12% 8% -- #!/usr/bin/perl use strict; use warnings; use Benchmark; open my $bit_bucket, ">", "/dev/null" or die "$!"; my $s = "foo"; my %subs = ( string => sub { print $bit_bucket "$s\n" }, comma => sub { print $bit_bucket $s, "\n" }, period => sub { print $bit_bucket $s . "\n" }, ); for my $sub (keys %subs) { print "$sub: ", $subs{$sub}(), "\n"; } Benchmark::cmpthese -1, \%subs; Ten items: Rate string comma period string 1052183/s -- -1% -2% comma 1061926/s 1% -- -1% period 1071850/s 2% 1% -- #!/usr/bin/perl use strict; use warnings; use Benchmark; open my $bit_bucket, ">", "/dev/null" or die "$!"; my @a = 1 .. 10; my %subs = ( string => sub { print $bit_bucket "$a[0]$a[1]$a[1]$a[2]$a[3]$a[4]$a[5]$a[6]$a[7]$a[8]$a[9]\n" }, comma => sub { print $bit_bucket $a[0],$a[1],$a[1],$a[2],$a[3],$a[4],$a[5],$a[6],$a[7],$a[8],$a[9], "\n" }, period => sub { print $bit_bucket $a[0].$a[1].$a[1].$a[2].$a[3].$a[4].$a[5].$a[6].$a[7].$a[8].$a[9]. "\n" }, ); for my $sub (keys %subs) { print "$sub: ", $subs{$sub}(), "\n"; } Benchmark::cmpthese -1, \%subs; -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/