Project

General

Profile

Actions

Feature #13436

open

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

Added by watson1978 (Shizuo Fujita) over 7 years ago. Updated over 5 years ago.

Status:
Open
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

Actions

Also available in: Atom PDF

Like0
Like0