in reply to Re: Upper case and chomp
in thread Upper case and chomp
I prefer using uc and a string compare like the OP suggested. I did a quick benchmark, and it looks like it's faster (negligible if no strings match, roughly double the speed if they all match.
if (uc($listaccountlocked[1]) eq 'TRUE) {
Trivial benchmark:
$ cat ucmatch_vs_regex.pl #!/usr/bin/perl use strict; use warnings; use Benchmark qw(:all); my @words; my @letters = qw( A B C D a b c d ); build_words(\@letters, \@letters, \@letters, \@letters); compare(); @letters = qw( d e f g h i k l ); build_words(\@letters, \@letters, \@letters, \@letters); compare(); @letters = qw( B B B B b b b b ); build_words( [qw(A A A A a a a a)], \@letters, [qw(C C C C c c c c)], +\@letters); compare(); sub build_words { @words=(); my ($rA, $rB, $rC, $rD) = @_; for my $a (@$rA) { for my $b (@$rB) { for my $c (@$rC) { for my $d (@$rD) { push @words, "$a$b$c$d"; } } } } } sub compare { my $v = @words; my $t = regex(); my $u = uccmp(); if ($t != $u) { die "Functions don't return the same value! regex=$t, +uccmp=$u\n"; } print "In $v words, $t are 'abcb'\n"; cmpthese(-5, { regex => sub { regex() }, uccmp => sub { uccmp() }, } ); } sub regex { my $cnt=0; for (@words) { ++$cnt if /abcb/i; } return $cnt; } sub uccmp { my $cnt=0; for (@words) { ++$cnt if uc($_) eq 'ABCB'; } return $cnt; } $ perl ucmatch_vs_regex.pl In 4096 words, 16 are 'abcb' Rate regex uccmp regex 939/s -- -7% uccmp 1008/s 7% -- In 4096 words, 0 are 'abcb' Rate uccmp regex uccmp 1028/s -- -3% regex 1055/s 3% -- In 4096 words, 4096 are 'abcb' Rate regex uccmp regex 414/s -- -52% uccmp 865/s 109% -- $
...roboticus
When your only tool is a hammer, all problems look like your thumb.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Upper case and chomp
by kennethk (Abbot) on Feb 10, 2011 at 17:09 UTC | |
by ikegami (Patriarch) on Feb 10, 2011 at 19:12 UTC | |
by roboticus (Chancellor) on Feb 10, 2011 at 22:57 UTC | |
|
Re^3: Upper case and chomp
by hbm (Hermit) on Feb 10, 2011 at 16:12 UTC | |
by ikegami (Patriarch) on Feb 10, 2011 at 19:02 UTC |