From c208d15ff7812c59b4506b1a5934ca5c755fd785 Mon Sep 17 00:00:00 2001 From: watson1978 Date: Sun, 21 May 2017 03:36:31 +0000 Subject: Improve Time#+ & Time#- performance * time.c (wadd): use internal addv() function to calculate internal value in Time object. On 64-bit machine, Time object might have Fixnum object internally by default and addv() can calculate Fixnum objects directly. * time.c (wsub): use internal subv() function due the same reason in above. Time#+ & Time#- will be faster around 15%. [ruby-dev:50036] [Bug #13357] [Fix GH-1547] ### Before user system total real Time#+ 0.820000 0.000000 0.820000 ( 0.818081) Time#- 0.810000 0.000000 0.810000 ( 0.813835) ### After user system total real Time#+ 0.710000 0.000000 0.710000 ( 0.710241) Time#- 0.710000 0.010000 0.720000 ( 0.714151) ### Test code require 'benchmark' Benchmark.bmbm do |x| x.report "Time#+" do t = Time.now 2000000.times do t + 1 end end x.report "Time#-" do t = Time.now 2000000.times do t - 1 end end end git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58829 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- time.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'time.c') diff --git a/time.c b/time.c index 495666c949..c97a2776da 100644 --- a/time.c +++ b/time.c @@ -345,33 +345,25 @@ wcmp(wideval_t wx, wideval_t wy) static wideval_t wadd(wideval_t wx, wideval_t wy) { - VALUE x; #if WIDEVALUE_IS_WIDER if (FIXWV_P(wx) && FIXWV_P(wy)) { wideint_t r = FIXWV2WINT(wx) + FIXWV2WINT(wy); return WINT2WV(r); } - else #endif - x = w2v(wx); - if (RB_TYPE_P(x, T_BIGNUM)) return v2w(rb_big_plus(x, w2v(wy))); - return v2w(rb_funcall(x, '+', 1, w2v(wy))); + return v2w(addv(w2v(wx), w2v(wy))); } static wideval_t wsub(wideval_t wx, wideval_t wy) { - VALUE x; #if WIDEVALUE_IS_WIDER if (FIXWV_P(wx) && FIXWV_P(wy)) { wideint_t r = FIXWV2WINT(wx) - FIXWV2WINT(wy); return WINT2WV(r); } - else #endif - x = w2v(wx); - if (RB_TYPE_P(x, T_BIGNUM)) return v2w(rb_big_minus(x, w2v(wy))); - return v2w(rb_funcall(x, '-', 1, w2v(wy))); + return v2w(subv(w2v(wx), w2v(wy))); } static wideval_t -- cgit v1.2.3