Issue #10785 has been updated by Nobuyoshi Nakada.


gogo tanaka wrote:
> Nobuyoshi Nakada wrote:
> > I don't think this is a sane idea
> 
> Hmm.. I believe using `(double)FIX2LONG(x)` instead of `RFLOAT_VALUE(rb_to_float(x))` for `Fixnum` dose make sense.
> Could I ask more detail about that?

I meant "redefinition of Integer#to_f".

> > redefinition of Integer#to_f won't be ignored
> 
> I ensure every value will be applied to `DBL2NUM` before return, so I think there is no compatibility problem.
> Is this what you mean?

I meant:

~~~
$ ruby -v -e 'p Math.log(1);class Fixnum; def to_f; self+1.0;end;end;p Math.log(1)'
ruby 2.3.0dev (2015-01-26 trunk 49408) [universal.x86_64-darwin14]
0.0
-e:1: warning: method redefined; discarding old to_f
0.6931471805599453
~~~

Wannabe's tweet may help us.
http://twitter.com/wannabe53/status/560441249274933248

----------------------------------------
Feature #10785: [PATCH1/1] [ja/en] 特定のケースでMath.cを3倍速く / Make Math.c 3 times faster in a certain situation
https://bugs.ruby-lang.org/issues/10785#change-51274

* Author: gogo tanaka
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
English follows japanese. Because my english is not good ;(

拙い英語のため両言語で補足をします.

`FIXNUM_P(x)`が真を取る際には `(double)FIX2LONG(x)` で、その他の場合は `NUM2DBL(x)` でCの値にするようにしてみました.
速度についてですが、引数が`Fixnum`, `Bignum`オブジェクトである時に限り有意な差を確認することが出来ました.
またその他のケースに対する影響は気になるほどではないと考えています.
色々至らぬところがある気がするので、何かありました際にはお気軽にお申し付け下さい.

Hi, now I use `(double)FIX2LONG(x)` instead of `NUM2DBL(x)` when `FIXNUM_P(x)` is true.

I can found it make almost all Math's module_function faster about 3 times, 
only when arg(s) is `Fixnum` or `Bignum` object.

i feel like I make some mistakes, so please feel free to let me know.

1試行を抜粋

    $ 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
```
  4.280000   0.060000   4.340000 (  4.690771)
  7.000000   0.090000   7.090000 (  7.240859)
  1.430000   0.020000   1.450000 (  1.644737)
  5.950000   0.100000   6.050000 (  6.236341)
```

# after
```
  1.340000   0.000000   1.340000 (  1.359663)
  3.770000   0.050000   3.820000 (  3.917417)
  1.370000   0.010000   1.380000 (  1.395028)
  6.080000   0.070000   6.150000 (  6.255340)
```

---Files--------------------------------
refactoring_math_c.patch (6.97 KB)


-- 
https://bugs.ruby-lang.org/