in reply to counting regex hits
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: counting regex hits Benchamark
by cees (Curate) on Mar 18, 2003 at 15:35 UTC | |
by Doc Technical (Initiate) on Mar 18, 2003 at 22:48 UTC | |
by Tomte (Priest) on Mar 19, 2003 at 09:17 UTC | |
by Doc Technical (Initiate) on Mar 19, 2003 at 20:05 UTC | |
|
Re^2: counting regex hits Benchamark (except)
by tye (Sage) on Mar 19, 2003 at 00:13 UTC | |
by Tomte (Priest) on Mar 19, 2003 at 07:38 UTC |