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.