in reply to Re: counting regex hits Benchamark
in thread counting regex hits

The only thing that I would change is the 'random feature' you put in the benchmark. This will throw off your results, since you are not providing the same value to each function. In the worst case the 'tr' function could get the shortest string and the 'm//' function gets the longest string every time.

Of course this ambiguity would be evened out since you do a million iterations, and I'll bet your results will not change much by fixing this. But, I would replace the random function and just loop through each item in every iteration.

cmpthese(100000, { 'tr' => sub { foreach (@rands) { my $x = tr/-//; } }, 'm//' => sub { foreach (@rands) { my $x = () = $_ =~ /-/g; } }, } );

This way you are guaranteed an even distribution of your sample data.

I agree with you that the results are impressive. Definately something to keep in the bin of useful perl knowledge...

Replies are listed 'Best First'.
Re: Re: Re: counting regex hits Benchamark
by Doc Technical (Initiate) on Mar 18, 2003 at 22:48 UTC
    No regex required:
    $strg = qq[This has- some- d-a-she-s -in it]; %cnt = (); @chrs = split('',$strg); foreach (@chrs) { $cnt{$_}++; } print qq[$cnt{'-'}\n];

      Why is this a reply to me?
      I do not get your point...

      ...but I "can't hold my water" anyways:
      If this is a reminder that I forgot in my (possibly silly ;-) benchmark a viable alternative, I assure you that a for-loop counting each characters occurences isn't a viable alternative if you're interested in /one/ character only, as is the OP. ¹

      kind regards,
      tomte


      ¹ silly me: ;-D

        Sorry I meant to reply to the initial question of the thread, not your response. Also I assumed that not invoking the regex engine would save execution time which is obviously incorrect.