I know that it is well-known that tr is faster than m//, nevertheless I thought I write my first Benchmark.pm-using, aehmm, well, benchmark :-)
#!/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; }, } );
I'd be glad to hear if I did something considerably stupid here :-), under this proviso the (well known!) results:
__END__ Benchmark: timing 1000000 iterations of m//, tr... m//: 57 wallclock secs (54.53 usr + 0.07 sys = 54.60 CPU) @ 18 +315.02/s (n=1000000) tr: 1 wallclock secs ( 1.92 usr + 0.00 sys = 1.92 CPU) @ 52 +0833.33/s (n=1000000) Rate m// tr m// 18315/s -- -96% tr 520833/s 2744% --
Quite impressive difference, isn't it?
regards,
tomte
In reply to Re: counting regex hits Benchamark
by Tomte
in thread counting regex hits
by Becky
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |