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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.