Ruby - Array.join versus String Concatenation (Efficiency)

后端 未结 5 1796
面向向阳花
面向向阳花 2021-02-05 06:56

I recall getting a scolding for concatenating Strings in Python once upon a time. I was told that it is more efficient to create an List of Strings in Python and join them later

5条回答
  •  我在风中等你
    2021-02-05 07:25

    I was just reading about this. Attahced is a link talking about it.

    Building-a-String-from-Parts

    From what I understand, in Python and Java strings are immutable objects unlike arrays, while in Ruby both strings and arrays are as mutable as each other. There might be a minimal difference in speed between using a String.concat or << method to form a string versus Array.join but it doesn't seem to be a big issue.

    I think the link will explain this a lot better than i did.

    Thanks,

    Martin

提交回复
热议问题