in reply to Re^2: Which is more faster? While or tr///
in thread Which is more faster? While or tr///

I can see four problems with the posted code that would make it run slower:

  1. You are using alternation when a character class would be faster.
  2. You are using capturing parentheses when you don't use the results of those captures.
  3. You are using the "+" Additive operator instead of the more efficient "+=" assignment operator.
  4. You are looping over the same string 28 or 29 times, depending on the value of $ftype, when you probably should only have to loop over the string twice.

For example:

while ($string=~m{(B|C|P|T|Z|a|b|d|h|k|n|p|q|u|v|x)}g){ $count=$count+($tbsz*0.5625); }

Would be more efficient as:

while ($string=~m{[BCPTZabdhknpquvx]}g){ $count+=($tbsz*0.5625); }

That would cover points 1, 2 and 3.    For point 4 you could use hash tables for the calculations, something like:

my %start_table = ( '\s' => 1, '<ems>' => 1, '<195>' => 1, '\.' => 0.25, '<ths>' => 0.25, '<193>' => 0.25, '<ens>' => 0.5, '<194>' => 0.5, ); my $start_lookup = join '|', keys %start_table; my %ftype_table = ( W => 1, '\s' => 1, '%' => 1, ### % is added temporarily for some testing purpose w => 0.84375, '\)' => 0.84375, ### need to escape meta-characters!!! '\(' => 0.84375, M => 0.8125, m => 0.8125, N => 0.7188, Q => 0.7188, # etc, ); my $ftype_lookup = join '', keys %ftype_table; my %non_ftype_table = ( W => 0.7844, w => 0.6999, A => 0.5656, X => 0.55, Q => 0.5469, O => 0.5469, R => 0.5375, K => 0.5375, Y => 0.5375, # etc. ); my $non_ftype_lookup = join '' keys %non_ftype_table; while ( $string =~ /($start_lookup)/og ) { $count += $tbsz * $start_table{ $1 }; } $string =~ s/<[A-Z\[\\\]\^_`a-z]+>//g; if ( $ftype == 1 ) { while ( $string =~ /([$ftype_lookup])/og ) { $count += $tbsz * $ftype_table{ $1 }; } else { while ( $string =~ /([$non_ftype_lookup])/og ) { $count += $tbsz * $non_ftype_table{ $1 }; } }