gpt4 book ai didi

Perl 字符串的性能

转载 作者:行者123 更新时间:2023-12-03 23:42:51 24 4
gpt4 key购买 nike

我已经遇到了很多 Perl 代码,它们以这种方式分解长字符串:

my $string = "Hi, I am a very long and chatty string that just won't";
$string .= " quit. I'm going to keep going, and going, and going,";
$string .= " kind of like the Energizer bunny. What are you going to";
$string .= " do about it?";

从我的 Java 背景来看,构建这样的字符串将是一个性能禁忌。 Perl 也是这样吗?在我的搜索中,我使用 join 阅读了该内容。在字符串数组上是连接字符串的最快方法,但是当您只想拆分字符串以提高可读性时怎么办?是否更好地写:
my $string = "Hi, I am a very long and chatty string that just won't" .
" quit. I'm going to keep going, and going, and going," .
" kind of like the Energizer bunny. What are you going to" .
" do about it?";

或者我使用 join ,或者应该怎么做?

最佳答案

Camel book, p 598 :

Prefer join("", . ..) to a series of concatenated strings. Multiple concatenations may cause strings to be copied back and forth multiple times. The join operator avoids this.

关于Perl 字符串的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3104493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com