On 3/13/07, Grant <[EMAIL PROTECTED]> wrote:
snip
> while( <SOCK> ) {
> push @out, $_ if /\S/;
> }
snip
Is that better than:
push @out, $_ unless /^\s*$/;
snip
They are functionally equivalent, but (at least on the version of perl
I have) /\S/ is faster:
mix:
Rate /^\s*$/ /\S/
/^\s*$/ 54.2/s -- -13%
/\S/ 62.1/s 15% --
all space:
Rate /^\s*$/ /\S/
/^\s*$/ 47.9/s -- -23%
/\S/ 62.1/s 30% --
all non-space:
Rate /^\s*$/ /\S/
/^\s*$/ 62.7/s -- -1%
/\S/ 63.6/s 1% --
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;
my @strings;
my $subs = {
'/\S/' => sub { my $bool; $bool = /\S/ for @strings },
'/^\s*$/' => sub { my $bool; $bool = /^\s*$/ for @strings }
};
print "mix:\n";
push @strings, random_string() for 1 .. 10_000;
Benchmark::cmpthese(-2, $subs);
print "all space:\n";
@strings = ();
push @strings, ' ' x (80 - int rand(80)) for 1 .. 10_000;
Benchmark::cmpthese(-2, $subs);
print "all non-space:\n";
@strings = ();
push @strings, 'a' x (80 - int rand(80)) for 1 .. 10_000;
Benchmark::cmpthese(-2, $subs);
sub random_string {
if (rand() < .5) {
return ' ' x (80 - int rand(80));
} else {
return 'a' x (80 - int rand(80));
}
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/