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

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
#!/usr/bin/perl -w use strict; use Benchmark qw(:all); my @rands = qw (A---B-B---B---D----F----G----H----J----K-----L---F-D-- +F---G--H---_R_-f-F-ff-----f ----F----G-- ----F----G------F----G------F----G-- ----F----G------F----G------F----G------F----G-- ----F----G------F----G------F----G------F----G-----F----G--- ----F----G-----F----G--- ----F----G------F----G------F----G--); my $max = @rands; cmpthese(1000000, { 'tr' => sub { $_ = $rands[int(rand($max))]; my $x = tr/-//; }, 'm//' => sub { $_ = $rands[int(rand($max))]; my $x = () = $_ =~ /-/g; }, 'for' => sub { $_ = $rands[int(rand($max))]; my %cnt = (); my @chrs = split '', $_; foreach (@chrs) { $cnt{$_}++; } my $x = $cnt{'-'}; }, } ); __END__ Benchmark: timing 1000000 iterations of for, m//, tr... for: 98 wallclock secs (95.68 usr + 0.09 sys = 95.77 CPU) @ 10 +441.68/s (n=1000000) m//: 55 wallclock secs (53.85 usr + 0.06 sys = 53.91 CPU) @ 18 +549.43/s (n=1000000) tr: 2 wallclock secs ( 1.81 usr + 0.00 sys = 1.81 CPU) @ 55 +2486.19/s (n=1000000) Rate for m// tr for 10442/s -- -44% -98% m// 18549/s 78% -- -97% tr 552486/s 5191% 2878% --

Replies are listed 'Best First'.
Re: Re*4: counting regex hits Benchamark
by Doc Technical (Initiate) on Mar 19, 2003 at 20:05 UTC
    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.