in reply to Improving dismal performance - Part 1
There are several reasons why this script is slow and Tie::Array is only one of them.
By keeping a bit of state and storing field values in a hash as you find them, you can completely eliminate the need to use an array and most of the internal if/else statements and loops as well. Here is much simplified version of your parser:
use strict; use warnings; sub printRecord; #-------------------------------------------------- # Parsing loop #-------------------------------------------------- my $fhOut = \*STDOUT; my $iLevel=0; my %hFields; while (my $sLine = <DATA>) { #if line defines the level, set level if ($sLine =~ /^\s*(?:Level|Record|Sub Record)\s+\(\d+\)/) { $iLevel++; } elsif ($sLine =~ /^\s*End of/) { $iLevel--; } else { my ($k, $v) = $sLine =~ /\s+\"(\w+)\"\s+=\s+\"([^"]*)\"/; $hFields{$k}=$v; } #if level back to 0, dump record if ($iLevel == 0) { printRecord($fhOut, \%hFields); %hFields=(); } } #-------------------------------------------------- # SUBROUTINE DEFINITIONS #-------------------------------------------------- sub printRecord { my ($fhOut, $hFields) = @_; my $sIOType = $hFields->{MSC_CDR_TYPE}; print $fhOut "RECORD\n"; print $fhOut "#addkey\n"; print $fhOut "#filename FF\n"; print $fhOut "#input_id 001\n"; print $fhOut "#input_type $sIOType\n"; print $fhOut "#output_id\n"; print $fhOut "#output_type $sIOType\n"; print $fhOut "#source_id SRC\n"; foreach my $k (sort keys %$hFields) { my $v = $hFields->{$k}; print $fhOut "F $k $v\n"; } print $fhOut ".\n"; } #cut and paste sample data from above __DATA__
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Improving dismal performance - Part 1
by PoorLuzer (Beadle) on May 12, 2009 at 23:03 UTC |