Leon's Blogging

Coding blogging for hackers.

Ruby - + vs Concat vs <<

| Comments

  • +: 每次都是不同的 object_id
  • concat: 每次都是回傳相同的 object_id,因此速度上會比較快
  • <<: 其實就是 concat 的 alias
  • interpolation: 最後是字串的插值,object_id 也會不一樣,但是速度是裡面最快的(fast-ruby),因為不需要再做處理

string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
a, b, c, d, e, f = 'a', 'b', 'c', 'd', 'e', 'f'

a.object_id    # => 70178339985920
a = a + b      # => "ab"
a.object_id    # => 70178335597640

b.object_id    # => 70178339985900
b.concat(c)    # => "bc"
b.object_id    # => 70178339985900

c.object_id    # => 70178339985880
c << d         # => "cd"
c.object_id    # => 70178339985880

e.object_id    # => 70178339797960
e = "#{e}#{f}" # => "ef"
e.object_id    # => 70178339730520

array

1
2
3
4
5
6
7
8
9
10
11
12
13
a, b, c, d, e, f = ['a'], ['b'], ['c'], ['d'], ['e'], ['f']

a.object_id # => 70114372794940
a = a + b # => ["a", "b"]
a.object_id # => 70114372758680

c.object_id # => 70114372794840
c.concat(d) # => ["c", "d"]
c.object_id # => 70114372794840

e.object_id # => 70114372794740
e << f # => ["e", ["f"]]
e.object_id # => 70114372794740

參考文件

Comments