[前][次][番号順一覧][スレッド一覧]

ruby-changes:58171

From: Watson <ko1@a...>
Date: Wed, 9 Oct 2019 12:25:29 +0900 (JST)
Subject: [ruby-changes:58171] 2d001003e4 (master): Improve performance of Array#sum with float elements (#1555)

https://git.ruby-lang.org/ruby.git/commit/?id=2d001003e4

From 2d001003e4b3a6c20ead09ed54b6726a7669f457 Mon Sep 17 00:00:00 2001
From: Watson <watson1978@g...>
Date: Wed, 9 Oct 2019 12:25:08 +0900
Subject: 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

diff --git a/array.c b/array.c
index 341edb5..19d9946 100644
--- a/array.c
+++ b/array.c
@@ -6620,12 +6620,12 @@ rb_ary_sum(int argc, VALUE *argv, VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L6620
          * See http://link.springer.com/article/10.1007/s0????-???-????-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);
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]