in reply to Re: Count number of occurrence of word
in thread Count number of occurrence of word

Hi, the strange thing is that, replicating your benchmark (together with a third solution), I get a much higher advantage for the 'a' solution than what you got:
$ perl -e 'use Benchmark(cmpthese); cmpthese(1000000, { "a" => sub { $_ = "this that this those this these."; $a = () = /this/g; }, "b" => sub { $_ = "this that this those this these."; $a++ while(s/this//); }, "c" => sub { $_ = "this that this those this these."; $a = s/this/this/g; } });' Rate a b c a 282406/s -- -28% -28% b 390778/s 38% -- -1% c 393391/s 39% 1% --
Note that I had to use one million iterations, with 100_000 I got the "too few iterations" warning.

Replies are listed 'Best First'.
Re^3: Count number of occurrence of word
by Lennotoecom (Pilgrim) on Mar 09, 2014 at 19:22 UTC
    ummm you mean a disadvantage?
    use Benchmark(cmpthese); cmpthese(1000000, { 'a' => sub { $_ = 'this that this those this these.'; $a = () = /this/g; }, 'b' => sub { $_ = 'this that this those this these.'; $a++ while(s/this//); }, 'c' => sub { $_ = 'this that this those this these.'; $a = s/this/this/g; } });
    my machine shows that your 'c' option is the fastest of these
    Rate a b c a 478240/s -- -4% -19% b 497018/s 4% -- -16% c 588235/s 23% 18% --
      Gosh, you are right, how silly on my part. I looked at the results too quickly and interpreted them the wrong way.
        happens to everybody,
        and still your option is the fastest one here ^)