Issue #13368 has been updated by watson1978 (Shizuo Fujita).


When I filed this ticket, I tried to run benchmark on macOS + clang only.
Then, I tried to do on 2 environments in additional.

I found what my patch is effective with clang envrionment only (such as macOS or FreeBSD which use clang as default compiler).

## macOS 10.12 + gcc 6.3.0
### Before
~~~
       user     system      total        real
   2.750000   0.000000   2.750000 (  2.757864)
~~~

### After
~~~
       user     system      total        real
   2.730000   0.010000   2.740000 (  2.740204)
~~~

## Ubuntu 16.04.4 + gcc 5.4.0
### Before
~~~
       user     system      total        real
   2.400000   0.000000   2.400000 (  2.395856)
~~~

### After
~~~
       user     system      total        real
   2.330000   0.000000   2.330000 (  2.327889)
~~~


----------------------------------------
Bug #13368: Improve performance of Array#sum with float elements
https://bugs.ruby-lang.org/issues/13368#change-64225

* Author: watson1978 (Shizuo Fujita)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
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



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