ruby-changes:46714
From: watson1978 <ko1@a...>
Date: Sun, 21 May 2017 12:36:36 +0900 (JST)
Subject: [ruby-changes:46714] watson1978:r58828 (trunk): Improve Time#<=> performance
watson1978 2017-05-21 12:36:30 +0900 (Sun, 21 May 2017) New Revision: 58828 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58828 Log: Improve Time#<=> performance * time.c (wcmp): use internal cmp() function for comparing internal Fixnum value in Time objects. On 64-bit machine, Time object might have Fixnum object internally by default and cmp() can compare the Fixnum objects directly. Time#<=> will be faster around 60% on 64-bit machine. * time.c (cmp): add optimized path for comparing internal Bignum value by using rb_big_cmp() API. On 32-bit machine, Time object might have Bignum object internally by default. Time#<=> will be faster around 50% on 32-bit machine. [ruby-dev:50034] [Bug #13354] [Fix GH-1546] ### Before user system total real Fixnum 1.410000 0.000000 1.410000 ( 1.407848) Bignum 1.550000 0.000000 1.550000 ( 1.549145) ### After user system total real Fixnum 0.880000 0.000000 0.880000 ( 0.886662) Bignum 1.050000 0.000000 1.050000 ( 1.047994) ### Test code require 'benchmark' Benchmark.bmbm do |x| x.report "Fixnum" do t1 = Time.now t2 = Time.now 10000000.times do t1 <=> t2 end end x.report "Bignum" do t1 = Time.at(2 ** 64) t2 = Time.at(2 ** 64 + 1) 10000000.times do t1 <=> t2 end end end Modified files: trunk/time.c Index: time.c =================================================================== --- time.c (revision 58827) +++ time.c (revision 58828) @@ -64,6 +64,7 @@ cmp(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/time.c#L64 return 1; return 0; } + if (RB_TYPE_P(x, T_BIGNUM)) return FIX2INT(rb_big_cmp(x, y)); return rb_cmpint(rb_funcall(x, idCmp, 1, y), x, y); } @@ -332,7 +333,7 @@ wcmp(wideval_t wx, wideval_t wy) https://github.com/ruby/ruby/blob/trunk/time.c#L333 #endif x = w2v(wx); y = w2v(wy); - return rb_cmpint(rb_funcall(x, idCmp, 1, y), x, y); + return cmp(x, y); } #define wne(x,y) (!weq((x),(y))) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/