Created
March 1, 2015 00:28
-
-
Save bayashi/ffc7edd26f609af54c46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use Benchmark qw(cmpthese); | |
my $h = '25'; | |
my $pattern = '((?:(?:'.$h.'[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 $regexp_o = qr/$pattern/o; | |
my $ipv4 = '192.168.1.1'; | |
cmpthese(0, +{ | |
'precompile' => sub { | |
$ipv4 =~ $regexp; | |
}, | |
'precompile_o' => sub { | |
$ipv4 =~ $regexp_o; | |
}, | |
'runtime' => sub { | |
$ipv4 =~ /$pattern/; | |
}, | |
'runtime_optimized' => sub { | |
$ipv4 =~ /$pattern/o; | |
}, | |
'runtime_static' => sub { | |
$ipv4 =~ /((?:(?:$h[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]?))/; | |
}, | |
'runtime_static_o' => sub { | |
$ipv4 =~ /((?:(?:$h[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]?))/o; | |
}, | |
}); |
正規表現周りのパフォチューだと
- マッチ試行回数自体を減らす
- 複数の正規表現まとめて一撃化したりとか
- マッチ対象文字列を小さくできたらする
- 正規表現で再帰的なことしてるとかは直せるなら直そう
みたいな感じで、precompile(できれば) とか /o(付けられれば) とかは
なんかもう最後の一押し的な感じですかねえ
文字列の存在見るだけなら正規表現じゃなくて index
でやろうとかもあるか
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ぼくのは Perl5.8.9 すね!
最初のコードで precompile の話と /o の話が混ざってるのを分離した方がよさそうかな。
もとから混ざってたから全条件並べただけだけどね!
https://gist.github.com/i110/901b94e7a86911c31930
この比較コードが precompile のベンチとしては良さそう。