Project

General

Profile

Actions

Feature #10785

closed

[PATCH1/1] [ja/en] 特定のケースでMath.cを3倍速く / Make Math.c 3 times faster in a certain situation

Added by gogotanaka (Kazuki Tanaka) almost 10 years ago. Updated almost 10 years ago.

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

Description

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) refactoring_math_c.patch gogotanaka (Kazuki Tanaka), 01/27/2015 02:22 AM

Updated by nobu (Nobuyoshi Nakada) almost 10 years ago

Although I don't think this is a sane idea, redefinition of Integer#to_f won't be ignored after your patch?

Updated by gogotanaka (Kazuki Tanaka) almost 10 years ago

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?

note: FIXNUM_P(x) can't be overhead

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?

thanks.

Updated by nobu (Nobuyoshi Nakada) almost 10 years ago

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

Updated by gogotanaka (Kazuki Tanaka) almost 10 years ago

I got your point, I'm sorry to cause you trouble.

But even now, Float#to_f is ignored. I think it's same for this case or we need to call #to_f for all argument.

ruby -v -e 'p Math.log(1.0);class Float; def to_f; self+1.0;end;end;p Math.log(1.0)'
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.0

Basically this message "warning: method redefined; discarding old to_f" is wrong, so we need to fix anyway.

Actions #6

Updated by nobu (Nobuyoshi Nakada) almost 10 years ago

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

Applied in changeset r49433.


math.c: Get_Double

  • math.c (Get_Double): direct casting from Fixnum to double.
    [Feature #10785]

Updated by gogotanaka (Kazuki Tanaka) almost 10 years ago

@Nobuyoshi Nakada san

Thank you for merging.

I'm gonna make docs be better and try to solve "warning" issue.

thanks.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0