Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Comparing strings case insensitive

by zemane (Novice)
on Sep 12, 2008 at 21:13 UTC ( [id://711009]=perlquestion: print w/replies, xml ) Need Help??

zemane has asked for the wisdom of the Perl Monks concerning the following question:

Which one is better:
if (lc $str1 eq lc $str2) { ... }
or
if ($str1 =~ /^$str2$/i) { ... }

Replies are listed 'Best First'.
Re: Comparing strings case insensitive
by sgifford (Prior) on Sep 12, 2008 at 22:25 UTC
    lc is much faster, it seems to me, though both are very fast (each of these tests below is for 1m comparisons, so regexp had 150K/s, and lc 1830K/s).
    Benchmark: timing 10 iterations of RegExp, lc... RegExp: 66 wallclock secs (66.20 CPU) @ 0.15/s (n=10) lc: 6 wallclock secs ( 5.47 CPU) @ 1.83/s (n=10)
Re: Comparing strings case insensitive
by Fletch (Bishop) on Sep 12, 2008 at 21:20 UTC

    The first is clearer and less error prone (woe unto you if $str2 ever contains metacharacters as is), but presuming a recent perl both should be equally speedy.

    Update: Wow, I must have heard wrong about the re speedups or something; I thought that they supposedly had gotten optimized so fixed strings were on the same order as a vanilla comparison but as the benchmark below shows that's not the case. So yeah, definitely option one.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      So the lower cased string comparison is more robust than the regexp?

        Yes, because lc "alligatorz" eq lc "a.*z" will properly compare as unequal, whereas "alligatorz" =~ /^a.*z$/i incorrectly (for what you're trying to do) will match. That's what I mentioned about robustness, but again with the proper application of \Q that wouldn't be an issue.

        That being said, I'd still use the lc $x eq lc $y version vice the \Q-ified regexp match as it's more explicit that you're doing a case insensitive comparison between fixed strings.

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

        So the lower cased string comparison is more robust than the regexp?

        Yes.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://711009]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-04-23 14:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found