in reply to Re: regex needed
in thread regex needed
I'm not sure there's any guarantee perl will do that optimization for you. If you want three parts, just tell it so:
Update: You seem to be right about the optimization. The following benchmark shows nearly identical times:$foo = (split/:/,$_,3)[1];
cchan.acsys.com.1.256% cat aaa3.pl use Benchmark; my $count = 1000000; our $str = "a:b:c:d:e:ff:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:v:w:x:y:z"; timethese($count, { 'slice' => sub { (undef, my $foo) = split /:/,$str; }, 'count' => sub { my $foo = (split/:/,$str,3)[1]; }, });
However, giving the split limit explicitly is clearer IMHO.Benchmark: timing 1000000 iterations of count, slice... count: 2 wallclock secs ( 3.18 usr + 0.01 sys = 3.19 CPU) @ 31 +3479.62/s (n=1000000) slice: 3 wallclock secs ( 3.04 usr + 0.06 sys = 3.10 CPU) @ 32 +2580.65/s (n=1000000)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: regex needed
by bart (Canon) on Mar 14, 2003 at 20:15 UTC |