想补充一下我在这方面的经验,因为我做了一些测试。我一直使用 Smart Match,最近厌倦了它会产生的警告。
我有一个1亿的文本文件,10个字符串。
Perl 脚本将 STDIN 转换为一个数组,并执行 3 种常用方法来查找数组中是否存在字符串。我尝试使用上面建议的哈希映射,但是与数组相比,哈希映射需要 3 倍的时间来生成。如果您正在对现有值进行广泛的测试,这种权衡在某些时候可能是可以的,因为对哈希的存在检查基本上是即时的。这也取决于您的数据源。
未来我计划主要使用 List::Util (any),因为它的未来证明是一个核心模块并且性能可靠。
#!/usr/bin/perl
use List::Util qw(any);
my @arr = qw(a b c d e);
if ( any { $_ eq 'd' } @arr ) {
print "Found.\n";
}
方法:
List::Util (any): if ( any { $_ eq $a } @arr ) { do something. }
Perl Smartmatch: if ( $b ~~ @arr ) { do something. }
Grep: if ( grep { $c eq $_ } @arr ) { do something. }
我搜索了我知道存在于 1,10,100,1000,10000,100000,1000000,10000000,100000000,100000000,100000000 个位置中的值。计时是通过 Time::HiRes 模块完成的。
我发现,如果您的大多数值都在数组的开头,则 smartmatch 将执行 List::Utils 方法。但是,如果您的大多数值位于中间或末尾或数组 List::Util 中不存在,则将无法执行。无论是否找到值,grep 似乎都会进行详尽的搜索。
更多输出细节:
Smart Match total: 5.939 seconds.
List::Util::any: 7.332 seconds.
Grep total: 39.553 seconds.
Array Generation Time: 30.315 seconds.
Searching 100000000 arr elements.
any Searching eavTa2eWr1
any Found - eavTa2eWr1.
Time: 0.540 seconds.
any Searching mhEusMj5E7
any Found - mhEusMj5E7.
Time: 0.358 seconds.
any Searching WGwHfJICK6
any Found - WGwHfJICK6.
Time: 0.364 seconds.
any Searching I48fNDYNKF
any Found - I48fNDYNKF.
Time: 0.359 seconds.
any Searching q3YVBTmX9J
any Found - q3YVBTmX9J.
Time: 0.357 seconds.
any Searching pw0J5vRCnW
any Found - pw0J5vRCnW.
Time: 0.358 seconds.
any Searching GNJP5flX5z
any Found - GNJP5flX5z.
Time: 0.392 seconds.
any Searching 3Mh0x0R3OC
any Found - 3Mh0x0R3OC.
Time: 0.649 seconds.
any Searching H5yxSA7eDx
any Found - H5yxSA7eDx.
Time: 3.473 seconds.
List::Util::any: 6.850 seconds.
###############################################################
SM Searching eavTa2eWr1
SM Found eavTa2eWr1.
Time: 0.000 seconds.
SM Searching mhEusMj5E7
SM Found mhEusMj5E7.
Time: 0.000 seconds.
SM Searching WGwHfJICK6
SM Found WGwHfJICK6.
Time: 0.000 seconds.
SM Searching I48fNDYNKF
SM Found I48fNDYNKF.
Time: 0.000 seconds.
SM Searching q3YVBTmX9J
SM Found q3YVBTmX9J.
Time: 0.001 seconds.
SM Searching pw0J5vRCnW
SM Found pw0J5vRCnW.
Time: 0.005 seconds.
SM Searching GNJP5flX5z
SM Found GNJP5flX5z.
Time: 0.054 seconds.
SM Searching 3Mh0x0R3OC
SM Found 3Mh0x0R3OC.
Time: 0.519 seconds.
SM Searching H5yxSA7eDx
SM Found H5yxSA7eDx.
Time: 5.083 seconds.
Smart Match total: 5.662 seconds.
###############################################################
Grep Searching eavTa2eWr1
Grep Found eavTa2eWr1.
Time: 4.648 seconds.
Grep Searching mhEusMj5E7
Grep Found mhEusMj5E7.
Time: 4.546 seconds.
Grep Searching WGwHfJICK6
Grep Found WGwHfJICK6.
Time: 4.295 seconds.
Grep Searching I48fNDYNKF
Grep Found I48fNDYNKF.
Time: 4.262 seconds.
Grep Searching q3YVBTmX9J
Grep Found q3YVBTmX9J.
Time: 4.282 seconds.
Grep Searching pw0J5vRCnW
Grep Found pw0J5vRCnW.
Time: 4.462 seconds.
Grep Searching GNJP5flX5z
Grep Found GNJP5flX5z.
Time: 4.420 seconds.
Grep Searching 3Mh0x0R3OC
Grep Found 3Mh0x0R3OC.
Time: 4.185 seconds.
Grep Searching H5yxSA7eDx
Grep Found H5yxSA7eDx.
Time: 4.112 seconds.
Grep total: 39.214 seconds.
Done.
检查不存在的值。
List::Util::any: 28.980 seconds.
Grep total: 34.790 seconds.
Smart Match total: 42.913 seconds.
Array Generation Time: 30.909 seconds.
Searching 100000000 arr elements.
any Searching eavTa2eWr1l
Time: 3.264 seconds.
any Searching mhEusMj5E7l
Time: 3.404 seconds.
any Searching WGwHfJICK6l
Time: 3.291 seconds.
any Searching I48fNDYNKFl
Time: 3.240 seconds.
any Searching q3YVBTmX9Jl
Time: 3.083 seconds.
any Searching pw0J5vRCnWl
Time: 3.247 seconds.
any Searching GNJP5flX5zl
Time: 3.180 seconds.
any Searching 3Mh0x0R3OCl
Time: 3.028 seconds.
any Searching H5yxSA7eDxl
Time: 3.243 seconds.
List::Util::any: 28.980 seconds.
###############################################################
SM Searching eavTa2eWr1l
Time: 4.620 seconds.
SM Searching mhEusMj5E7l
Time: 4.783 seconds.
SM Searching WGwHfJICK6l
Time: 4.899 seconds.
SM Searching I48fNDYNKFl
Time: 4.902 seconds.
SM Searching q3YVBTmX9Jl
Time: 4.863 seconds.
SM Searching pw0J5vRCnWl
Time: 4.646 seconds.
SM Searching GNJP5flX5zl
Time: 4.751 seconds.
SM Searching 3Mh0x0R3OCl
Time: 4.666 seconds.
SM Searching H5yxSA7eDxl
Time: 4.782 seconds.
Smart Match total: 42.913 seconds.
###############################################################
Grep Searching eavTa2eWr1l
Time: 4.034 seconds.
Grep Searching mhEusMj5E7l
Time: 3.849 seconds.
Grep Searching WGwHfJICK6l
Time: 3.837 seconds.
Grep Searching I48fNDYNKFl
Time: 3.822 seconds.
Grep Searching q3YVBTmX9Jl
Time: 3.923 seconds.
Grep Searching pw0J5vRCnWl
Time: 3.825 seconds.
Grep Searching GNJP5flX5zl
Time: 3.994 seconds.
Grep Searching 3Mh0x0R3OCl
Time: 3.846 seconds.
Grep Searching H5yxSA7eDxl
Time: 4.174 seconds.
Grep total: 35.303 seconds.
Done.