Ruby Array concat versus + speed?

后端 未结 4 543
萌比男神i
萌比男神i 2021-02-12 06:00

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

4条回答
  •  粉色の甜心
    2021-02-12 06:51

    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.

提交回复
热议问题