fastruby / fastruby/fast-ruby

Remove `String#casecmp` part

Đang mở
#123 6 bình luận 2 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ả

The `String#casecmp` method does not work with Unicode (even in Ruby 2.4.1), this is written in the [documentation](http://ruby-doc.org/core-2.4.1/String.html#method-i-casecmp).

Example:

```ruby
'Привет'.casecmp('привет') # => -1
```

There is a method `String#casecmp?`, which [works with Unicode](http://ruby-doc.org/core-2.4.1/String.html#method-i-casecmp-3F).

Example:

```ruby
'Привет'.casecmp?('привет') # => true
```

But `String.casecmp?` is slower:

```
Warming up --------------------------------------
String#downcase + == 233.440k i/100ms
String#casecmp 274.247k i/100ms
String#casecmp? 219.906k i/100ms
Calculating -------------------------------------
String#downcase + == 5.746M (± 1.7%) i/s - 28.947M in 5.039252s
String#casecmp 6.942M (± 1.8%) i/s - 34.829M in 5.019073s
String#casecmp? 4.517M (± 2.6%) i/s - 22.650M in 5.017864s

Comparison:
String#casecmp: 6941676.9 i/s
String#downcase + ==: 5745893.8 i/s - 1.21x slower
String#casecmp?: 4517314.3 i/s - 1.54x slower
```

Code

```ruby
require 'benchmark/ips'

SLUG = 'ABCD'

def slow
SLUG.downcase == 'abcd'
end

def fast
SLUG.casecmp('abcd') == 0
end

def another
SLUG.casecmp?('abcd')
end

Benchmark.ips do |x|
x.report('String#downcase + ==') { slow }
x.report('String#casecmp') { fast }
x.report('String#casecmp?') { another }
x.compare!
end
```

So, `String#downcase + ==` is good compromise.

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.