From 8d9d7c929e4987b4332e33c5126b620ba7c0c2b3 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 1 Apr 2017 13:03:34 +0900 Subject: Improve Time.new() performance using division; fix #3561 --- mrbgems/mruby-time/src/time.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/mrbgems/mruby-time/src/time.c b/mrbgems/mruby-time/src/time.c index 908fe77f8..de94f7137 100644 --- a/mrbgems/mruby-time/src/time.c +++ b/mrbgems/mruby-time/src/time.c @@ -11,6 +11,8 @@ #include #include +#define NDIV(x,y) (-(-((x)+1)/(y))-1) + #if _MSC_VER < 1800 double round(double x) { if (x >= 0.0) { @@ -237,13 +239,15 @@ time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone) tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(struct mrb_time)); tm->sec = tsec; tm->usec = (time_t)llround((sec - tm->sec) * 1.0e6 + usec); - while (tm->usec < 0) { - tm->sec--; - tm->usec += 1000000; - } - while (tm->usec >= 1000000) { - tm->sec++; - tm->usec -= 1000000; + if (tm->usec < 0) { + long sec2 = NDIV(usec,1000000); /* negative div */ + tm->usec -= sec2 * 1000000; + tm->sec += sec2; + } + if (tm->usec >= 1000000) { + long sec2 = usec / 1000000; + tm->usec -= sec2 * 1000000; + tm->sec += sec2; } tm->timezone = timezone; time_update_datetime(mrb, tm); -- cgit v1.2.3