本家エロチック街道
最近更新まったくしてませんが。
最近更新まったくしてませんが。
2007-12-03
■[Perl]日本語含む正規表現が遅いときがあるよ
こんな現象見つけた。
use utf8; use Time::HiRes; use Benchmark qw(:all :hireswallclock); sub five { my $a = 'あいうえお'; my $b = 'あれれ'; ($b =~ /^\s*$a\s*$/) ? 1 : 0; } sub three { my $a = 'あいう'; my $b = 'あれれ'; ($b =~ /^\s*$a\s*$/) ? 1 : 0; } my $res = timethese(10, { five => \&five, three => \&three, }); cmpthese($res);
まあベンチマークとるまでもなく体感で違うんですが、手元のCentOSでの結果。
Benchmark: timing 10 iterations of five, three...
five: 21.6958 wallclock secs (21.69 usr + 0.00 sys = 21.69 CPU) @ 0.46/s (n=10)
three: 5.07832e-05 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU)
(warning: too few iterations for a reliable count)
s/iter five three
five 2.17 -- -100%
three 1.00e-16 2168999999999999744% --
わーい。つまりひらがな5文字にすると2秒ぐらい掛るように。これ1文字足したり引いたりいろいろすると1秒以下になったりします。微妙なとこでプランが変わるような感触。
Perlは5.8.8、関係あるのかどうかEncodeは2.18。
これ、既知のアレなのかな。
まあ、このコードの場合先に$bの先頭末尾から空白文字を削ってeqで比べたほうがいい、ということでいいんですが、ちょっとなにやら不穏な気配がしたもので。日本語含みの場合気をつけなきゃいけないってこと?
正規表現先頭の「^\s*」が怪しいのかな。
encode_utf8してから比べるようにすることでも回避できるようですが…あまりやりたくないなあ。
トラックバック - http://d.hatena.ne.jp/head/20071203/perl
リンク元
- 159 http://head.as.wakwak.ne.jp/games/
- 83 http://ezsch.ezweb.ne.jp/search/ezGoogleMain.php?query=エロチックCG&ct=&pd=1&sr=0000
- 51 http://iddy.jp/profile/head/
- 37 http://search.yahoo.co.jp/search?p=エロチック街道&search.x=1&fr=top_ga1&tid=top_ga1&ei=UTF-8
- 33 http://head.aboutme.jp/profile
- 20 http://www.google.co.jp/search?q=一蘭&hl=ja&lr=&start=10&sa=N
- 16 http://subtech.g.hatena.ne.jp/otsune/20070415/publishtwitterdiff
- 16 http://www.google.com/search?hl=ja&lr=lang_ja&ie=UTF-8&oe=UTF-8&q=一蘭&num=50
- 13 http://search.yahoo.co.jp/search?p=エロティック&search_x=1&tid=top_ga1&ei=UTF-8&qrw=0&pstart=1&fr=top_ga1&b=21
- 13 http://www.google.com/search?num=100&hl=ja&ie=sjis&oe=sjis&q=エロチックGIF&filter=0
