aboutsummaryrefslogtreecommitdiffstats
path: root/time.c
diff options
context:
space:
mode:
authorwatson1978 <watson1978@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-05-21 03:36:30 +0000
committerwatson1978 <watson1978@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-05-21 03:36:30 +0000
commitcf21f0a87d393f9bc52e4cce4a70ce90165c9bc5 (patch)
tree891683377274cab12693472ee5614614d8f939c3 /time.c
parent7bd2b4901825ff79c01e04679580536f2390b035 (diff)
downloadruby-cf21f0a87d393f9bc52e4cce4a70ce90165c9bc5.tar.gz
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
Diffstat (limited to 'time.c')
-rw-r--r--time.c3
1 files changed, 2 insertions, 1 deletions
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)))