Andrew savige wrote:
> 
> sub a1 { my @lines = split(/^/, $x, -1); chomp(@lines) }
> sub a2 { my @lines = $x eq "" ? () : $x =~ /^.*/mg }
> sub j1 { my @lines = map { chomp; $_ } split /^/, $x, -1 }
> # w1 is Perl 5.8.0 only
> sub w1 { open(my $fh, "<", \$x); my @lines = <$fh>; chomp(@lines) }
> timethese(600000, {
>    'a1'     => \&a1,
>    'a2'     => \&a2,
>    'j1'     => \&j1,
>    'w1'     => \&w1,
> });
> 
> Results on Linux, Perl 5.8.0:
>  a1: 27 wallclock secs (15.06 usr +  0.01 sys = 15.07 CPU)
>  a2: 42 wallclock secs (24.06 usr +  0.04 sys = 24.10 CPU)
>  j1: 49 wallclock secs (27.84 usr +  0.04 sys = 27.88 CPU)
>  w1: 101 wallclock secs (62.74 usr +  0.04 sys = 62.78 CPU)
> 
> Why is a1 fastest? Not sure, but I noticed in the Camel re split:
> "the patterns /\s+/, /^/ and / / are specially optimized".

You can speed up a1 a hair by chomp'ing the assignment.

sub a1 { chomp( my @lines = split(/^/, $x, -1) ) }


You can also speed up j1 by using map without the braces.

sub j1 { my @lines = map chomp && $_, split /^/, $x, -1 }



John
-- 
use Perl;
program
fulfillment

Reply via email to