以下の内容はhttps://blog.nishimu.land/entry/20090221/1235218220より取得しました。


Which is the fastest division method which returns Float.

Ruby has some division method but I don't know that the fastest one. I wanna know that.

I did the benchmark as bellow,

require 'benchmark'

aset = (1..1000).to_a

Benchmark.bmbm do |x|
  x.report('/')    { aset.each_cons(2) {|x, y| y./ x.to_f } }
  x.report('fdiv') { aset.each_cons(2) {|x, y| y.fdiv y } }
  x.report('quo')  { aset.each_cons(2) {|x, y| y.quo y }; require 'rational' }
end

I mesuared /, fdiv and quo method speed and get results like that

Rehearsal ------------------------------------
/      0.000000   0.000000   0.000000 (  0.001958)
fdiv   0.000000   0.000000   0.000000 (  0.001350)
quo    0.010000   0.000000   0.010000 (  0.003500)
--------------------------- total: 0.010000sec

           user     system      total        real
/      0.000000   0.000000   0.000000 (  0.001644)
fdiv   0.000000   0.000000   0.000000 (  0.000716)
quo    0.020000   0.000000   0.020000 (  0.018965)

fdiv is the fastest!


...hey! look at the live result, quo is very slow.

why?
Because I puted a littele trick on benchmark code. yes, that is require 'rational'.
quo becomes method which returns rational if load rational library.
and becomes too slow method.


Here is the bottom line, English is very difficult.




以上の内容はhttps://blog.nishimu.land/entry/20090221/1235218220より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14