Project

General

Profile

Feature #13436

Improve performance of Array#<=> with Fixnum/Float/String elements

Added by watson1978 (Shizuo Fujita) over 2 years ago. Updated 2 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:80688]

Description

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

History

#1

Updated by jeremyevans0 (Jeremy Evans) 2 months ago

  • Backport deleted (2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN)
  • Tracker changed from Bug to Feature

Also available in: Atom PDF