Peter Guzis wrote:
> Below is one possible optimization: the elimination of regular expressions.
My version (it handles quoted fields on both ends, but there could be some
better way to strip the "s on the substr/index) :
Results:
Rate do_index do_regex
do_index 33473/s -- -13%
do_regex 38438/s 15% --
use strict;
use warnings;
use Benchmark qw(cmpthese);
my $test = $debug;
my @init = (
'"YField1,"Field2","Field3","Field4",Field5"',
'"YField1,Field2,Field3,Field4,Field5"',
'"YField1,Field2,Field3,"Field4",Field5"',
'"YField1,Field2,Field3,Field4,Field5"',
'"NField1",Field2,Field3,Field4,"Field5"',
'"YField1,Field2,Field3,Field4,Field5"',
'"NField1","Field2","Field3","Field4","Field5"',
'"YField1,Field2,Field3,Field4,Field5"',
'"NField1","Field2","Field3","Field4","Field5"',
'"YField1,Field2,Field3,Field4,Field5"',
);
# benchmark
cmpthese ($test ? 1 : 1000000, {
do_index => \&do_index,
do_regex => \&do_regex,
});
exit;
# find records enclosed in quotes (substr/index)
sub do_index {
my @records = @init;
foreach (@records) {
# test for for bracketing "s
if (substr ($_, 0, 1) eq '"' and substr ($_, -1, 1) eq '"') {
$_ = substr $_, 1, -1;
my $i1 = index $_, '"';
my $i2 = index $_, ',';
my $r1 = rindex $_, '"';
my $r2 = rindex $_, ',';
# test for first and/or last field quoting
if ($i1 != -1 and $i1 < $i2 and $r1 != -1 and $r1 > $r2) {
printf "No - %s\n", substr $_, 1, -1 if $test;
} else {
printf "Yes - %s\n", substr $_, 1, -1 if $test;
}
} else {
printf "No - %s\n", $_ if $test;
}
}
}
# find records enclosed in quotes (regex way)
sub do_regex {
my @records = @init;
foreach (@records) {
# test for for bracketing "s and first and/or last field quoting
if (/^"([^"]+,.*,[^"]+)"$/) {
printf "Yes - %s\n", $1 if $test;
} else {
printf "No - %s\n", $_ if $test;
}
}
}
__END__
--
,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED]
(_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED]
/ ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs