On Sat, May 26, 2012 at 05:52:19PM +0100, Rob Dixon wrote:
> On 26/05/2012 14:07, pa...@fsmail.net wrote:
>> From: "Rob Dixon"<rob.di...@gmx.com>
>>> On 26/05/2012 13:51, pa...@fsmail.net wrote:
>>>>
>>>>  split is slower than the correct regex matching.
>>>
>>> That is complete nonsense. Can you show a benchmark that supports your
>>> claim?
> I do know, and it is nonsense.
>
> The benchmark below shows that split is better than twice as fast as a
> regex for the application I have chosen.
>
> If you can rewrite the program to show a diffferent use of regexes where
> they are faster than an equivalent split then I will take it back.
>
> Please stop deliberately spreading misinformation.
> cmpthese(-5, {
>   split => sub {
>     my @array = split ' ', $str;
>   },
>   regex => sub {
>     my @array = $str =~ /\S+/g;
>   },
> })

Curious, splitting on single char - what split has been optimized for, the awk 
behavior,
but the regex comparsion is splitting on non-whitespace, the inverse of what 
split is doing.  
Correcting \S to \s returns similar results. 

But that raises the question of what happens when not splitting on the highly 
optimized space scenario.
First, let's see what happens when both split and the regex act on \S:
cmpthese(-5, {
  split => sub {
    my @array = split /\S+/, $str;
  },
  regex => sub {
    my @array = $str =~ /\S+/g;
  },
})
         Rate regex split
regex 44162/s    --   -1%
split 44501/s    1%    --

Now, what happens if our whitespace is mixed between tabs and spaces:
         Rate regex split
regex 40420/s    --  -34%
split 61438/s   52%    --

How about the not uncommon case of simple CSV?

#  $str .= ' ' x (rand 10 + 1);
$str .= ",";
# change the /\S+/ to /,/

         Rate regex split
regex 63021/s    --   -8%
split 68790/s    9%    --


Perhaps the lesson is split on whitespace else regex


-- 
            Michael Rasmussen, Portland Oregon  
      Other Adventures: http://www.jamhome.us/ or http://westy.saunter.us/
Fortune Cookie Fortune du courrier:
There are those who listen and those who wait to talk.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to