#! perl -slw use strict; use Readonly; use Benchmark qw[ cmpthese ]; use constant { SUBSTITUTES => { # substitute these 'DUTCH' => 'NETHERLANDS', 'GERMANY' => 'DEUTSCHLAND', 'AUST.' => 'AUSTRALIA', }, SKIPWORDS => { # skip these 'BANK' => 1, 'CORP' => 1, 'GOVERNMENT' => 1, 'GOVT' => 1, 'LIMITED' => 1, 'LTD' => 1, 'NPV' => 1, 'COM' => 1, }, }; sub wordsConstant { return [ map { SUBSTITUTES->{$_} or $_ } grep { !SKIPWORDS->{$_} } split /\s+/, shift ]; } Readonly::Hash my %SUBSTITUTES => ( 'DUTCH' => 'NETHERLANDS', 'GERMANY' => 'DEUTSCHLAND', 'AUST.' => 'AUSTRALIA', ); Readonly::Hash my %SKIPWORDS => ( 'BANK' => 1, 'CORP' => 1, 'GOVERNMENT' => 1, 'GOVT' => 1, 'LIMITED' => 1, 'LTD' => 1, 'NPV' => 1, 'COM' => 1, ); sub wordsReadonly { return [ map { $SUBSTITUTES{$_} or $_ } grep { ! $SKIPWORDS{$_} } split /\s+/, shift ]; } sub wordsInline { return [ map { { # substitute these 'DUTCH' => 'NETHERLANDS', 'GERMANY' => 'DEUTSCHLAND', 'AUST.' => 'AUSTRALIA', }->{$_} or $_ } grep { !{ # skip these 'BANK' => 1, 'CORP' => 1, 'GOVERNMENT' => 1, 'GOVT' => 1, 'LIMITED' => 1, 'LTD' => 1, 'NPV' => 1, 'COM' => 1, }->{$_} } split /\s+/, shift ]; } our $testData = uc <<'EOD'; The quick dutch fox jumps over the lazy government dog. The quick german fox jumps over the lazy bank dog. The quick aust. fox limited jumps over the lazy corporate dog corp. EOD cmpthese -1, { constant => q[ wordsConstant( $testData ) ], inline => q[ wordsInline( $testData ) ], readonly => q[ wordsReadonly( $testData ) ], }; __END__ C:\test>559911 Rate readonly inline constant readonly 2133/s -- -22% -81% inline 2745/s 29% -- -75% constant 10957/s 414% 299% --