From 5d3fac0db9b2756d630abc9844a7a1450a8fdcba Mon Sep 17 00:00:00 2001 From: normal Date: Mon, 10 Apr 2017 18:08:16 +0000 Subject: 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 for the patch. From: Watson git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58308 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- time.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/time.c b/time.c index 0d93ec65ff..04c111a0c6 100644 --- a/time.c +++ b/time.c @@ -103,7 +103,17 @@ mul(VALUE x, VALUE y) 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) -- cgit v1.2.3