Skip to content

Instantly share code, notes, and snippets.

@i110
Last active August 29, 2015 14:16
Show Gist options
  • Save i110/28abb5692ee1ed2bbb0a to your computer and use it in GitHub Desktop.
Save i110/28abb5692ee1ed2bbb0a to your computer and use it in GitHub Desktop.
結論
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use feature 'say';
use Benchmark qw(cmpthese);
say $];
my $pattern = '((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))';
my $regexp = qr/$pattern/;
my $ipv4 = '192.168.1.1';
cmpthese(0, +{
'reref_expand_pattern' => sub {
$ipv4 =~ qr/$pattern/;
},
'reref_static' => sub {
$ipv4 =~ qr/((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))/;
},
'expand_reref' => sub {
$ipv4 =~ /$regexp/;
},
'precompile' => sub {
$ipv4 =~ $regexp;
},
'expand_pattern' => sub {
$ipv4 =~ /$pattern/;
},
'static' => sub {
$ipv4 =~ /((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))/;
},
});
@i110
Copy link
Author

i110 commented Mar 1, 2015

5.16.3の出来事

                          Rate reref_static reref_expand_pattern expand_reref precompile expand_pattern static
reref_static          580891/s           --                  -0%         -38%       -40%           -55%   -57%
reref_expand_pattern  581025/s           0%                   --         -38%       -40%           -55%   -57%
expand_reref          944303/s          63%                  63%           --        -2%           -27%   -30%
precompile            960475/s          65%                  65%           2%         --           -26%   -29%
expand_pattern       1295003/s         123%                 123%          37%        35%             --    -5%
static               1357889/s         134%                 134%          44%        41%             5%     --

@i110
Copy link
Author

i110 commented Mar 1, 2015

結論

  • 正規表現のリファレンス生成(qr)&デリファレンスが遅い
  • プリコンパイルはしないほうが良い
    • 当然プリコンパイル自体が悪いわけではない
    • が、プリコンパイルしてから再利用しようとするとリファレンス化せざるを得ない(よね?)
    • コンパイル回数を減らす効果よりもデリファレンスのoverheadのほうが大きいので結局意味無い
    • ただしものっっそい複雑でコンパイルのコストが高い正規表現とかの場合は話が変わるかもしれない
  • その他の最適化めいたものは基本的に変化なし
    • 変数展開とかも微々たるもの
    • oオプションも確かにobsoletedのようである
(´・ω・`).;:…(´・ω...:.;::..(´・;::: .:.;: サラサラ..

@i110
Copy link
Author

i110 commented Mar 1, 2015

ふと思い立って色んな長さのパターンで試してみた

5倍の長さ(上記パターンの5回繰り返し)

                           Rate reref_expand_pattern reref_static expand_reref precompile expand_pattern static
reref_expand_pattern  1031053/s                   --          -5%         -69%       -72%           -92%   -97%
reref_static          1082539/s                   5%           --         -68%       -71%           -92%   -97%
expand_reref          3352499/s                 225%         210%           --       -10%           -74%   -91%
precompile            3729822/s                 262%         245%          11%         --           -71%   -90%
expand_pattern       12923405/s                1153%        1094%         285%       246%             --   -65%
static               37147799/s                3503%        3332%        1008%       896%           187%     --

ipv4の最初の数字のみ /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/

                          Rate reref_expand_pattern reref_static precompile expand_reref expand_pattern static
reref_expand_pattern  809465/s                   --          -9%       -49%         -50%           -74%   -76%
reref_static          891487/s                  10%           --       -44%         -45%           -71%   -74%
precompile           1582768/s                  96%          78%         --          -2%           -48%   -54%
expand_reref         1613850/s                  99%          81%         2%           --           -47%   -53%
expand_pattern       3055255/s                 277%         243%        93%          89%             --   -11%
static               3440637/s                 325%         286%       117%         113%            13%     --

1文字のみ /1/

                           Rate reref_expand_pattern reref_static precompile expand_reref expand_pattern static
reref_expand_pattern  1037153/s                   --          -6%       -63%         -64%           -90%   -92%
reref_static          1099748/s                   6%           --       -61%         -62%           -89%   -92%
precompile            2802480/s                 170%         155%         --          -2%           -72%   -79%
expand_reref          2861048/s                 176%         160%         2%           --           -72%   -79%
expand_pattern       10190294/s                 883%         827%       264%         256%             --   -25%
static               13508011/s                1202%        1128%       382%         372%            33%     --

全方位的にベタ書き最強でしたとさ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment