Ruby Array concat versus + speed?

后端 未结 2 1928
谎友^
谎友^ 2021-02-12 06:09

I did small performance test of Ruby\'s array concat() vs + operation and concat() was way too fast.

I however am not clear on why

2条回答
  •  太阳男子
    2021-02-12 06:36

    According to the Ruby docs, the difference is:

    Array#+ :

    Concatenation — Returns a new array built by concatenating the two arrays together to produce a third array.

    Array#concat :

    Array#concat : Appends the elements of other_ary to self.

    So the + operator will create a new array each time it is called (which is expensive), while concat only appends the new element.

提交回复
热议问题