我想优化这个Perl子:
push_csv($string,$addthis,$position);
用于将字符串放在CSV字符串中.
例如,如果$string="one,two,,four"; $addthis="three"; $position=2;
那时push_csv($string,$addthis,$position)将改变的价值$string = "one,two,three,four";
sub push_csv {
my @fields = split /,/, $_[0]; # split original string by commas;
$_[1] =~ s/,//g; # remove commas in $addthis
$fields[$_[2]] = $_[1]; # put the $addthis string into
# the array position $position.
$_[0] = join ",", @fields; # join the array with commas back
# into the string.
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码中的瓶颈,因为它需要被称为几百万次.
如果你精通Perl,你能看看它,并提出优化/替代方案吗?提前致谢!:)
编辑:转换为@fields并返回字符串需要时间,我只想到一种方法来加快它我连续多个子调用.拆分一次,然后将多个东西推入阵列,然后在最后加入一次.
| 归档时间: |
|
| 查看次数: |
632 次 |
| 最近记录: |