Issue #13436 has been reported by watson1978 (Shizuo Fujita).

----------------------------------------
Bug #13436: Improve performance of Array#<=> with Fixnum/Float/String elements
https://bugs.ruby-lang.org/issues/13436

* Author: watson1978 (Shizuo Fujita)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
Array#<=> will be ~7.5 times faster with Fixnum elements.

### Before
~~~
               user     system      total        real
Fixnum     2.020000   0.010000   2.030000 (  2.022351)
Float      2.180000   0.000000   2.180000 (  2.185891)
String     2.290000   0.000000   2.290000 (  2.289221)
~~~

### After
~~~
               user     system      total        real
Fixnum     0.260000   0.000000   0.260000 (  0.266339)
Float      0.760000   0.000000   0.760000 (  0.754436)
String     0.520000   0.000000   0.520000 (  0.529152)
~~~

### Test code
~~~
require 'benchmark'

Benchmark.bmbm do |x|

  x.report "Fixnum" do
    ary1 = Array.new(1000) { rand(1000) }
    ary2 = ary1.dup

    50000.times do
      ary1 <=> ary2
    end
  end

  x.report "Float" do
    ary1 = Array.new(1000) { rand }
    ary2 = ary1.dup

    50000.times do
      ary1 <=> ary2
    end
  end

  x.report "String" do
    ary1 = Array.new(1000) { rand(1000).to_s }
    ary2 = ary1.dup

    50000.times do
      ary1 <=> ary2
    end
  end
end
~~~

### Patch
https://github.com/ruby/ruby/pull/1583



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>