| | use POSIX; | use POSIX (); | | 0.516s | 0.355s | | use Socket; | use Socket (); | | 0.270s | 0.231s |
POSIX 默认倒出一大堆符号变量。假如您使用了不倒出,它在开始就 少 30% 的时间。 Socket 能少 15% 的时间。
regexps- avoid $&
- The
$& variable returns the last text successfully matched in any regular expression. It's not lexically scoped, so unlike the match variables $1 etc it isn't reset when you leave a block. This means that to be correct perl has to keep track of it from any match, as perl has no idea when it might be needed. As it involves taking a copy of the matched string, it's expensive for perl to keep track of. If you never mention $&, then perl knows it can cheat and never store it. But if you (or any module) mentions $& anywhere then perl has to keep track of it throughout the script, which slows things down. So it's a good idea to capture the whole match explicitly if that's what you need. $text =~ /.* rules/;
$line = $&; # Now every match will copy $& - slow $text =~ /(.* rules)/;
$line = $1; # Didn't mention $& - fast - avoid use English;
use English gives helpful long names to all the punctuation variables. Unfortunately that includes aliasing $& to $MATCH which makes perl think that it needs to copy every match into $&, even if you script never actually uses it. In perl 5.8 you can say use English '-no_match_vars'; to avoid mentioning the naughty "word", but this isn't available in earlier versions of perl. - avoid needless captures
- Are you using parentheses for capturing, or just for grouping? Capturing involves perl copying the matched string into
$1 etc, so it all you need is grouping use a the non-capturing (?:...) instead of the capturing (...). - /.../o;
- If you define scalars with building blocks for your regexps, and then make your final regexp by interpolating them, then your final regexp isn't going to change. However, perl doesn't realise this, because it sees that there are interpolated scalars each time it meets your regexp, and has no idea that their contents are the same as before. If your regexp doesn't change, then use the
/o flag to tell perl, and it will never waste time checking or recompiling it.
|
| 共13页: 上一页 [1] [2] [3] [4] [5] 6 [7] [8] [9] [10] [11] [12] [13] 下一页 |
评论加载中…