久しぶりにモダパー読み直してたらqr//と文字列の正規表現マッチ速度が出てたので、新しい5.16とかも出てきてるし、やってみた。
use strict; use Benchmark qw/ cmpthese /; my $re_string = 'a.c'; my $re_regexp = qr/a.c/; sub match { my $re = shift; "abc" =~ /$re/; } cmpthese(1_000_000, { string => sub { match($re_string) }, regexp => sub { match($re_regexp) }, });
$ perlbrew exec perl qr_speed.pl
perl-5.8.9
==========
Rate regexp string
regexp 2173913/s -- -0%
string 2173913/s 0% --
perl-5.10.1
==========
Rate regexp string
regexp 1086957/s -- -45%
string 1960784/s 80% --
perl-5.12.4
==========
Rate regexp string
regexp 1176471/s -- -38%
string 1886792/s 60% --
perl-5.14.2
==========
Rate regexp string
regexp 1136364/s -- -51%
string 2325581/s 105% --
perl-5.16.0
==========
Rate regexp string
regexp 1063830/s -- -50%
string 2127660/s 100% --
perl-5.17.0
==========
Rate regexp string
regexp 1030928/s -- -44%
string 1851852/s 80% --
5.8.9以外、stringの方がかなり速い。
Macのdarwinでは
qr//オペレーターに対してまだチューニングが充分されていないということが読み取れます。
(P248)
ということでいいのだろうか?
- 作者: 牧大輔
- 出版社/メーカー: 翔泳社
- 発売日: 2009/02/10
- メディア: 大型本
- 購入: 25人 クリック: 534回
- この商品を含むブログ (113件) を見る
2012/6/4追記
CentOS5(32bit)でやってみた。
$ perlbrew exec perl qr_speed.pl
perl-5.8.8
==========
Rate string regexp
string 628931/s -- -5%
regexp 662252/s 5% --
perl-5.8.9
==========
Rate string regexp
string 704225/s -- -12%
regexp 800000/s 14% --
perl-5.10.1
==========
Rate regexp string
regexp 543478/s -- -19%
string 671141/s 23% --
perl-5.12.4
==========
Rate regexp string
regexp 546448/s -- -20%
string 684932/s 25% --
perl-5.14.2
==========
Rate regexp string
regexp 546448/s -- -25%
string 724638/s 33% --
perl-5.16.0
==========
Rate regexp string
regexp 502513/s -- -26%
string 680272/s 35% --
perl-5.17.0
==========
Rate regexp string
regexp 490196/s -- -29%
string 694444/s 42% --Macよりはstringとregexpの差が小さい。5.8.xだけregexpが上回る、というのは変わらないみたいだけれど。