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

ruby-changes:46193

From: normal <ko1@a...>
Date: Tue, 11 Apr 2017 03:08:23 +0900 (JST)
Subject: [ruby-changes:46193] normal:r58308 (trunk): time.c: Improve Time#to_i performance

normal	2017-04-11 03:08:16 +0900 (Tue, 11 Apr 2017)

  New Revision: 58308

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58308

  Log:
    time.c: Improve Time#to_i performance
    
    Time#to_i will be faster around 80% (on 64-bit platforms).
    
    * Before
           user     system      total        real
       2.840000   0.000000   2.840000 (  2.847238)
    
    * After
           user     system      total        real
       1.600000   0.000000   1.600000 (  1.598911)
    
    * Test code
    require 'benchmark'
    
    Benchmark.bmbm do |x|
      x.report do
        t = Time.now
        20000000.times do
          t.to_i
        end
      end
    end
    
    * time.c (_div): new function avoid rb_funcall
      (div): replace with new _div function
      [ruby-core:80636] [Bug #13418]
      Thanks to Watson <watson1978@g...> for the patch.
    
    From: Watson <watson1978@g...>

  Modified files:
    trunk/time.c
Index: time.c
===================================================================
--- time.c	(revision 58307)
+++ time.c	(revision 58308)
@@ -103,7 +103,17 @@ mul(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/time.c#L103
     return rb_funcall(x, '*', 1, y);
 }
 
-#define div(x,y) (rb_funcall((x), id_div, 1, (y)))
+static VALUE
+_div(VALUE x, VALUE y)
+{
+    if (FIXNUM_P(x) && FIXNUM_P(y)) {
+	return rb_fix_div_fix(x, y);
+    }
+    if (RB_TYPE_P(x, T_BIGNUM))
+        return rb_big_div(x, y);
+    return rb_funcall(x, id_div, 1, y);
+}
+#define div(x,y) _div(x,y)
 
 static VALUE
 mod(VALUE x, VALUE y)

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

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