Perl 的 unpack() 比 substr() 快吗? [英] Is Perl's unpack() ever faster than substr()?
问题描述
我多次读到 unpack()
比 substr()
快,尤其是当子字符串数量增加时.但是,该基准表明并非如此.是我的基准测试有缺陷,还是 unpack()
所谓的性能优势是旧版 Perl 的延续?
Several times I've read that unpack()
is faster than substr()
, especially as the number of substrings increases. However, this benchmark suggests otherwise. Is my benchmark flawed, or is the alleged performance advantage of unpack()
a holdover from older versions of Perl?
use strict;
use warnings;
use Benchmark;
my ($data, $format_string, $n_substrings);
my %methods = (
unpack => sub { return unpack $format_string, $data },
substr => sub { return map {substr $data, $_, 1} 0 .. $n_substrings - 1 },
);
for my $exp (1 .. 5){
$n_substrings = 10 ** $exp;
print $n_substrings, "
";
$format_string = 'a1' x $n_substrings;
$data = 9 x $n_substrings;
Benchmark::cmpthese -2, \%methods;
}
输出(在 Windows 上):
Output (on Windows):
10
Rate unpack substr
unpack 131588/s -- -52%
substr 276802/s 110% --
100
Rate unpack substr
unpack 13660/s -- -57%
substr 31636/s 132% --
1000
Rate unpack substr
unpack 1027/s -- -68%
substr 3166/s 208% --
10000
Rate unpack substr
unpack 84.4/s -- -74%
substr 322/s 281% --
100000
Rate unpack substr
unpack 5.46/s -- -82%
substr 30.1/s 452% --
正如一些答案中指出的那样,unpack()
在 Windows 上表现不佳.这是在solaris 机器上的输出——不是那么决定性,但substr()
仍然在竞争中获胜:
As pointed out in some answers, unpack()
does poorly on Windows. Here's the output on a solaris machine -- not nearly so decisive, but substr()
still wins the foot race:
10
Rate unpack substr
unpack 202274/s -- -4%
substr 210818/s 4% --
100
Rate unpack substr
unpack 22015/s -- -9%
substr 24322/s 10% --
1000
Rate unpack substr
unpack 2259/s -- -9%
substr 2481/s 10% --
10000
Rate unpack substr
unpack 225/s -- -9%
substr 247/s 9% --
100000
Rate unpack substr
unpack 22.0/s -- -10%
substr 24.4/s 11% --
推荐答案
自从提出这个问题后,我在各种条件下对 substr
和 unpack
进行了多次基准测试.以下是我学到的一些东西:
Since asking this question, I have benchmarked substr
against unpack
several more times, under various conditions. Here are a few things I've learned:
不要以某种方式设置基准在 void 中调用 Perl 函数上下文(正如我在最初的问题中所做的那样;请参阅来自 dlowe 的有用回应).一些 Perl 函数有调用时的优化void 上下文(以及这些优化似乎因操作系统而异),可能歪曲您的基准测试结果.
Do not set up the benchmark in a way that calls Perl functions in void context (as I did in my original question; see the helpful response from dlowe). Some Perl functions have optimizations when they are called in void context (and these optimizations appear to vary by OS), potentially skewing your benchmarking results.
如果您使用 substr
涉及循环(例如,迭代列位置列表),解压
总是更快.但是,那substr
在这方面的明显缓慢情况是由于开销循环,而不是 substr
本身.
If your use of substr
involves
looping (for example, iterating over
a list of column locations), unpack
is always faster. However, the
apparent slowness of substr
in this
situation is due to the overhead of
the loop, not substr
itself.
如果只需要几个字段,substr
通常更快或解包
速度快.
If just a few fields are required,
substr
is generally faster or as
fast as unpack
.
如果有多个字段需要,头对头的比较unpack
和等价物之间substr
调用的数量不变与字段数一样多增加:两种方法都变成以同样的速度变慢.
If more than a few fields are
required, head-to-head comparisons
between unpack
and an equivalent
number of substr
calls do not vary
much as the number of fields
increases: both approaches become
slower at the same rate.
结果可能因操作系统而异.在我的 Windows XP 机器上,unpack
每当超过时都有轻微的优势需要几个字段.在我们的我工作场所的 Solaris 机器,substr
总是更快,甚至进入数百个字段.
Results can vary by operating system.
On my Windows XP machine, unpack
had a slight edge whenever more than
a few fields were needed. On our
Solaris machines at my workplace,
substr
was always faster, even into
hundreds of fields.
底线:unpack
与 substr
的性能不是一个很大的问题,无论字段的数量如何.使用哪种方法会产生最清晰的代码.但是,如果您发现自己在循环结构中使用了 substr
,那么切换到 unpack
会显着提高速度.
Bottom line: the performance of unpack
vs. substr
is not a very big issue, regardless of the number of fields. Use whichever approach results in the clearest code. If you find yourself using substr
in a looping construct, however, switching to unpack
will result in a noteworthy speed boost.
这篇关于Perl 的 unpack() 比 substr() 快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!