I have constructed the following test case to compare the performance of simple regular expression and simple eq test:

use Benchmark; @switch_me = qw / bob frnak 1 roger james /; timethese (1000000, { 'test_regex' => ' &test_regex; ', 'test_eq' => ' &test_eq; ' }); sub test_regex() { for(@switch_me) { /bob/ && do { break }; /frnak/ && do { break }; $_ == 1 && do { break }; } } sub test_eq() { for(@switch_me) { $_ eq "bob" && do { break }; $_ eq "frnak" && do { break }; $_ == 1 && do { break }; } }
The two subroutines are identical except for the string testing bit. The following are the test results:
Benchmark: timing 1000000 iterations of test_eq, test_regex... test_eq: 8 wallclock secs ( 8.45 usr + 0.01 sys = 8.46 CPU) @ 118161.41/s (n=1000000) test_regex: 10 wallclock secs ( 9.22 usr + 0.01 sys = 9.23 CPU) @ 108307.16/s (n=1000000)
It's about (118161.41-108307.16)/108307.16 = 10% speed increase in using eq than using regular expression. It's not a great increase, but it's nevertheless faster to use eq than regular expression.

In reply to Re: Re (4): Simple Switch statement by Roger
in thread Simple Switch statement by knexus

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.