Perl sub优化使用split将字符串推入csv

Dav*_*ave 5 perl

我想优化这个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并返回字符串需要时间,我只想到一种方法来加快它我连续多个子调用.拆分一次,然后将多个东西推入阵列,然后在最后加入一次.

Pla*_*ure 0

您不认为使用数组和splice, 并且仅用于join在末尾创建逗号分隔可能更容易吗?

s///如果这是代码中的主要瓶颈,我真的不认为重复使用是一个好主意。