2016-01-22 27 views

回答

4

对此,您不需要substr

$str =~ /^/

要检查是否有许多空白字符一个开始:

$str =~ /^\s/ 
2

不需要正则表达式是:

substr($str, 0, 1) eq " " 
3

你不需要substr这个正则表达式!

ord($str) == 32 

而且,如果你在做这些比较的十亿,你应该注意到一个英俊的性能提升,以及:

use Benchmark qw(cmpthese); 

my $str = "  hello"; 

cmpthese(0, { 
    regex => sub { $str =~ /^/}, 
    substr => sub { substr($str, 0, 1) eq ' ' }, 
    ord => sub { ord($str) == 32 }, 
}); 

结果:

   Rate regex substr ord 
regex 6473675/s  -- -43% -70% 
substr 11300632/s 75%  -- -48% 
ord 21653474/s 234% 92%  -- 
相关问题