diff options
author | Watson <watson1978@gmail.com> | 2019-10-09 12:25:08 +0900 |
---|---|---|
committer | Kenta Murata <mrkn@users.noreply.github.com> | 2019-10-09 12:25:07 +0900 |
commit | 2d001003e4b3a6c20ead09ed54b6726a7669f457 (patch) | |
tree | 91b56301a072c1c0bdaabb886d1ef43741bd3396 /array.c | |
parent | a14cc07f2ffc704b73ba4b96543e2f85c3ed1921 (diff) | |
download | ruby-2d001003e4b3a6c20ead09ed54b6726a7669f457.tar.gz |
Improve performance of Array#sum with float elements (#1555)
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
Diffstat (limited to 'array.c')
-rw-r--r-- | array.c | 2 |
1 files changed, 1 insertions, 1 deletions
@@ -6620,12 +6620,12 @@ rb_ary_sum(int argc, VALUE *argv, VALUE ary) * See http://link.springer.com/article/10.1007/s00607-005-0139-x */ double f, c; + double x, t; f = NUM2DBL(v); c = 0.0; goto has_float_value; for (; i < RARRAY_LEN(ary); i++) { - double x, t; e = RARRAY_AREF(ary, i); if (block_given) e = rb_yield(e); |