Perl的unpack()是否比substr()更快? [英] Is Perl's unpack() ever faster than substr()?

查看:86
本文介绍了Perl的unpack()是否比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, "\n";
    $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%     --

推荐答案

由于提出了这个问题,因此我在各种条件下针对unpacksubstr作了多次基准测试.这是我学到的一些东西:

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函数具有 调用时进行优化 无效上下文(以及这些优化 似乎因操作系统而异),可能 扭曲基准测试结果.

  • 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涉及 循环(例如,遍历 列位置列表),unpack 总是更快.但是,那 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通常更快或更 与unpack一样快.

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.

底线:unpacksubstr的性能无关紧要,无论字段数如何.使用任何一种方法都能得到最清晰的代码.但是,如果您发现在循环结构中使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆