Why are you using a regex to check a one-character value?

if( $vartocheck eq lc($firstchar) ) { ++$foo; }

Update: The benchmark is mightier than the word:

#! /usr/bin/perl -w use strict; use Benchmark; use vars qw/$vartocheck $firstchar/; $vartocheck = 'x'; $firstchar = 'y'; my $iters = shift || 10000; timethese $iters, { 'eq !=' => sub { return $vartocheck eq lc($firstchar) }, 're !=' => sub { return $vartocheck =~ /\Q$firstchar\E/i }, }; $firstchar = 'x'; timethese $iters, { 'eq ==' => sub { return $vartocheck ne lc($firstchar) }, 're ==' => sub { return $vartocheck !~ /\Q$firstchar\E/i }, }; __END__ % perl eqre 10000000 Benchmark: timing 10000000 iterations of eq !=, re !=... eq !=: 8 wallclock secs ( 8.00 usr + 0.02 sys = 8.02 CPU) @ 12 +46882.79/s (n=10000000) re !=: 14 wallclock secs (13.74 usr + 0.10 sys = 13.84 CPU) @ 72 +2543.35/s (n=10000000) Benchmark: timing 10000000 iterations of eq ==, re ==... eq ==: 8 wallclock secs ( 6.98 usr + 0.04 sys = 7.02 CPU) @ 14 +24501.42/s (n=10000000) re ==: 17 wallclock secs (16.76 usr + -0.01 sys = 16.75 CPU) @ 59 +7014.93/s (n=10000000)

Update: Taking tye's advice below to heart, I anchored the regex, even though that was not the case in the initial question. It is interesting to note that the results for the regex are worse than before. The only other thing to check would be to use lexicals instead of package variables. This, of course, is left as an exercise to the reader.

timethese $iters, { 'eq !=' => sub { return $vartocheck eq lc($firstchar) }, 're !=' => sub { return $vartocheck =~ /^\Q$firstchar\E$/i }, }; __END__ % perl eqre 10000000 Benchmark: timing 10000000 iterations of eq !=, re !=... eq !=: 7 wallclock secs ( 7.42 usr + 0.00 sys = 7.42 CPU) @ 13 +47708.89/s (n=10000000) re !=: 26 wallclock secs (25.89 usr + 0.01 sys = 25.90 CPU) @ 38 +6100.39/s (n=10000000) Benchmark: timing 10000000 iterations of eq ==, re ==... eq ==: 8 wallclock secs ( 6.60 usr + 0.00 sys = 6.60 CPU) @ 15 +15151.52/s (n=10000000) re ==: 27 wallclock secs (27.77 usr + 0.00 sys = 27.77 CPU) @ 36 +0100.83/s (n=10000000)

--
g r i n d e r

In reply to Re: Regex question by grinder
in thread Regex question by Xxaxx

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.