Project

General

Profile

Actions

Feature #13368

closed

Improve performance of Array#sum with float elements

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

Status:
Closed
Target version:
-
[ruby-dev:50045]

Description

The declaration of local variable in loop, it will initialize local variable for each run of the loop with clang generated code.
So, it shouldn't declare the local variable in heavy loop.

Array#sum with float elements will be faster around 30%.

Before

       user     system      total        real
   3.320000   0.010000   3.330000 (  3.336088)

After

       user     system      total        real
   2.590000   0.010000   2.600000 (  2.602399)

Test code

require 'benchmark'

Benchmark.bmbm do |x|
  ary = []
  10000.times { ary << Random.rand }

  x.report do
    50000.times do
      ary.sum
    end
  end

end

Patch

https://github.com/ruby/ruby/pull/1555

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0