最佳答案
在 Ruby 中快速生成长字符串的最佳方法是什么? 这个方法有效,但速度非常慢:
str = ""
length = 100000
(1..length).each {|i| str += "0"}
我还注意到,创建一个长度合适的字符串,然后将其附加到现有的字符串上,直到达到所需的长度,这样工作起来要快得多:
str = ""
incrementor = ""
length = 100000
(1..1000).each {|i| incrementor += "0"}
(1..100).each {|i| str += incrementor}
还有别的建议吗?