From 92ea637c624f3efa7e8ff0f24ff89aa21193a2d1 Mon Sep 17 00:00:00 2001 From: watson1978 Date: Sun, 21 May 2017 03:36:30 +0000 Subject: 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 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58828 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- time.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'time.c') diff --git a/time.c b/time.c index d9a2156874..495666c949 100644 --- a/time.c +++ b/time.c @@ -64,6 +64,7 @@ cmp(VALUE x, VALUE y) 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) #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))) -- cgit v1.2.3