Well, my first thought was simply to write something that was simple, and not to worry about the speed. It depends on how many tens of million times this is going to happen before the speed even becomes an issue.

However, since you asked:

use Benchmark; $a="Hello"; timethese(10_000_000, { eq => sub { return ($a eq "" || $a eq "?") }, regexp => sub { $a=~/^\??$/ } } );
gives
Benchmark: timing 10000000 iterations of eq, regexp... eq: 7 wallclock secs ( 3.51 usr + -0.01 sys = 3.50 CPU) @ 28 +57142.86/s (n=10000000) regexp: 22 wallclock secs (10.15 usr + 0.13 sys = 10.28 CPU) @ 97 +2762.65/s (n=10000000)
The figures don't change (significantly) when $a="?". When it's an empty string, the equality takes half the time (since the second equality is never used), and the regexp is slightly slower (because it has to backtrack). undef makes both slightly slower.

--
Tommy
Too stupid to live.
Too stubborn to die.


In reply to Re: Re: Re: Check on if statement by tommyw
in thread Check on if statement by Anonymous Monk

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.