fastruby / fastruby/fast-ruby

string/concatenation.rb tests are misleading

Đang mở
#64 3 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Ruby
Star
5.7k
Fork
371
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

Hi, the tests in `string/concatenation.rb` are quite misleading.

The fast method consists of this

```
def fast
'foo' 'bar'
end
```

That's not concatenating during calling of `fast` but on parsing the code. If you write another method just returning `foobar`, it is as fast as this method.

So I think this is not fair comparison and what you usually want is to concatenate two variables during runtime.

For this use case `concat` and `<<` are calling the same code, so they have the same performance and both are fine if you want to change the string on the left and not just get two strings concatenated. If you want a new string you can use `+`.

Some better test could be to compare `+` and String interpolation

```
Benchmark.ips do |x|
foo = 'foo'
bar = 'bar'

x.report('String#+') do
foo + bar
end

x.report('String interpolation') do
"#{foo}#{bar}"
end

x.compare!
end
```

This still has the difference that interpolation can handle nil values, while `+` cannot.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.