分割字符串


#!/usr/bin/perl -w

###########################################
#
# 将一个字符串分成3个3个一组的6种方法
# 执行的速度依次增加
#
###########################################

use Benchmark qw(cmpthese);

cmpthese 100, {
'Name1' => '$a="123456789" x 1000;($b=$a)=~s/(.{3})/$1|/g;$c=join ",",split /\|/,$b;',
'Name2' => '$a="123456789" x 1000;@i=0..int((length($a)-1)/3);map {$_*=3} @i;$c=join ",",map {substr($a,$_,3)} @i;',
'Name3' => '$a="123456789" x 1000;@i=map {$_*3} 0..int((length($a)-1)/3);$c=join ",",map {substr($a,$_,3)} @i;',
'Name4' => '$a="123456789" x 1000;$c=join ",",map {substr($a,$_,3)} map {$_*3} 0..int((length($a)-1)/3);',
'Name5' => '$a="123456789" x 1000;$c=join ",",$a=~m/(.{1,3})/g;',
'Name6' => '$a="123456789" x 1000;$c=join ",",unpack("(a3)*",$a);',
};

你可能感兴趣的:(C++,c,C#,perl)