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)
#!/usr/bin/perl
use warnings;
use strict;
use Benchmark;
use constant SIZE => 1000;
our @a1 = map { "a".$_ } (1..SIZE);
our @a2 = map { "A".$_ } (1..SIZE);
sub use_re
{
my $count = 0;
foreach my $i (@a1) {
foreach my $j (@a2) {
if ($i =~ /^\Q$j\E$/i) {
$count++;
}
}
}
die unless $count == SIZE;
}
sub use_lc
{
my $count = 0;
foreach my $i (@a1) {
foreach my $j (@a2) {
if (lc $i eq lc $j) {
$count++;
}
}
}
die unless $count == SIZE;
}
timethese(10, {
RegExp => \&use_re,
lc => \&use_lc,
});
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.