aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWatson <watson1978@gmail.com>2019-10-09 12:25:08 +0900
committerKenta Murata <mrkn@users.noreply.github.com>2019-10-09 12:25:07 +0900
commit2d001003e4b3a6c20ead09ed54b6726a7669f457 (patch)
tree91b56301a072c1c0bdaabb886d1ef43741bd3396
parenta14cc07f2ffc704b73ba4b96543e2f85c3ed1921 (diff)
downloadruby-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
-rw-r--r--array.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/array.c b/array.c
index 341edb5aaf..19d9946a01 100644
--- a/array.c
+++ b/array.c
@@ -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);