Project

General

Profile

Actions

Feature #10800

closed

[PRRF][PATCH] Make math.c twice as faster when passed Bignum

Added by gogotanaka (Kazuki Tanaka) about 9 years ago. Updated about 9 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:67889]

Description

This ticket is continued from Feature 10785

My patch last time is only for Fixnum, but now I'd like to do same thing for Bignum without loosing others performance.

And this time I follow r49434 not to ignore redefinition of Integer#to_f ;)

thanks.


Files

introduce_num2dbl_with_to_f_func.patch (7.38 KB) introduce_num2dbl_with_to_f_func.patch gogotanaka (Kazuki Tanaka), 01/29/2015 10:08 PM
fix_indent_for_MISC_10800.patch (683 Bytes) fix_indent_for_MISC_10800.patch gogotanaka (Kazuki Tanaka), 01/30/2015 01:44 AM

Updated by gogotanaka (Kazuki Tanaka) about 9 years ago

$ cat bench.rb
require 'benchmark'

puts Benchmark.measure { 10000000.times { Math.cos(1) } }
puts Benchmark.measure { 10000000.times { Math.cos((1 << 100)) } }
puts Benchmark.measure { 10000000.times { Math.cos(1.0) } }
puts Benchmark.measure { 10000000.times { Math.cos(1/3r) } }

Before

  1.370000   0.000000   1.370000 (  1.379540)
  6.700000   0.080000   6.780000 (  7.085635)
  1.380000   0.020000   1.400000 (  1.484075)
  6.460000   0.110000   6.570000 (  7.262271)

After

  1.370000   0.000000   1.370000 (  1.391781)
  3.590000   0.020000   3.610000 (  3.650833)
  1.400000   0.010000   1.410000 (  1.416726)
  6.110000   0.040000   6.150000 (  6.208570)

Updated by gogotanaka (Kazuki Tanaka) about 9 years ago

Hi, I just fix indent my patch "introduce_num2dbl_with_to_f_func.patch".

If you prefer merge it to one patch, please feel free to let me know.
(I can't remove assets uploaded, so I upload patch separately to avoid making you confused)

thanks.

Updated by nobu (Nobuyoshi Nakada) about 9 years ago

  • Tracker changed from Misc to Feature
Actions #4

Updated by nobu (Nobuyoshi Nakada) about 9 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

Applied in changeset r49449.


math.c: optimization for Bignum

  • math.c (num2dbl_with_to_f): make faster when Bignum passed by
    direct conversion using rb_big2dbl(). [Feature #10800]

Updated by gogotanaka (Kazuki Tanaka) about 9 years ago

Hi! After r49449, I found math.c become 5.7 times faster than before.
note: in a certain case (Math.atan2 with Fixnum)

In almost all case, every function become about 3 times faster than before.

(And now Ruby's math module faster than python's one in my benchmark, but we don’t need to care about it so much)

math.c with Fixnum(1)

:Func    :Before(s)            :After(s)             :Before/After(s/s)         (:python 3.4.2_1)
cos      4.254130927001825     1.4405686919926666    2.9530913386138486 times   (2.5527)
sin      4.291027050989214     1.4243739590019686    3.012570556959532  times   (2.6044)
tan      5.363125992007554     1.483381660989835     3.6154727627068226 times   (2.7025)
atan2    7.793055543996161     1.355327188008232     5.749944082099257  times   (2.3252)
acos     4.32658559900301      1.2158808639942436    3.55839599678369   times   (2.3867)
asin     4.435602433994063     1.186041908003972     3.7398361761591388 times   (2.5280)
atan     5.748181078990456     1.365189026997541     4.210538588661546  times   (3.5160)
cosh     4.990852664006525     1.2583572969888337    3.9661649961813765 times   (2.4602)
sinh     4.523930807001307     1.29845637800463      3.4840837810457232 times   (2.5097)
tanh     4.194435634999536     1.262307046999922     3.322833097516404  times   (2.4466)
acosh    4.406813007997698     1.236766300004092     3.5631735825783073 times   (2.5998)
asinh    4.644120037002722     1.7610739079973428    2.637095476750275  times   (2.9141)
atanh    4.88491953999619      1.5865220309933648    3.079011475773588  times   (2.0711)
exp      4.4360210559971165    1.262295284002903     3.514249884488133  times   (2.3970)
log      8.53709495300427      2.3806999689986696    3.585960038717101  times   (3.8041)
log2     4.5818921600002795    1.3234373809973476    3.4621148123739314 times   (2.2458)
log10    4.361282436992042     1.306003783000051     3.339410263401872  times   (2.2278)
sqrt     4.195985846003168     1.1582538549992023    3.6226823920271416 times   (2.2647)
cbrt     4.281482355989283     1.3084301960043376    3.272228330608696  times
frexp    4.681357052002568     1.7626637079956708    2.6558424223334978 times   (3.7055)
ldexp    4.358438926996314     1.3060880839911988    3.3370176027313665 times   (3.7046)
hypot    7.374471293005627     1.348440486995969     5.468888960338427  times   (3.7265)
erf      4.596977610999602     1.6560243620042456    2.7759118262221656 times   (5.6961)
erfc     4.516037146997405     1.5825385909993201    2.8536663640825837 times   (5.7214)
gamma    4.0430225580057595    1.194721624997328     3.3840707939096704 times   (6.6276)
lgamma   4.750712952998583     1.8684836480097147    2.542549921728768  times   (9.2180)

I leave code which measures this benchmark here(https://gist.github.com/gogotanaka/379b00397d494a2b22d2)

I'm gonna do same thing for Rational and produce more proper and general benchmark.

@Nobuyoshi Nakada san
thank you for improving and merging my patch!

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0