犬ターネット

in_array か preg_match か

2011-07-04 php

単語存在チェックするのに

とするか

とするかって話。

PHPer だと多分 in_array を使う人が多い気がする。でも Perl 触ってると preg_match を使いたくなる。

じゃあどっちが処理速いかって話をしよう。in_array と preg_match 使って、3000単語の中から特定の単語探してみる。

まずは in_array の場合。

次は preg_match の場合。

ほんっっっっっっのわずかだけど preg_match が速い。じゃあ10000単語に増やしてみよう。

in_array の場合。

preg_match の場合。

エラー。

preg_match の場合、ディフォルトだと正規表現パターンに64Kまでしか使えない。それ以上使いたい場合はコンパイルしなおせと。でも動作遅くなるよって書いてある

HANDLING VERY LARGE PATTERNS

Within a compiled pattern, offset values are used to point from one part to another
(for example, from an opening parenthesis to an alternation metacharacter).
By default, two-byte values are used for these offsets,leading to a maximum size for a compiled pattern of around 64K.
This is sufficient to handle all but the most gigantic patterns.
Nevertheless, some people do want to process truyl enormous patterns,so it is possible to compile PCRE to use three-byte or  four-byte offsets by adding a setting such as

--with-link-size=3

to the configure command.The value given must be 2, 3, or 4. Using longer offsets slows down the operation of PCRE because it has to load additional bytes when handling them.

ええと、どっちが速いかって話か。別にどっちでもいい。


textarea 用の zen-coding プラグイン

このサイト