Note that there are some explanatory texts on larger screens.

plurals
  1. POquick/fast integer multiplication in ruby?
    text
    copied!<p>I am trying to make a quick/efficient Mandelbrot implementation in Ruby. A long long time ago, one way to speed it up was using fixed point integers instead of floats.</p> <p>So i made the following benchmark, comparing float and integer raising to a square, using multiplication or square ** operand.</p> <pre><code>require 'benchmark' Benchmark.bmbm(10) do |x| x.report("float-multip") do for z in 0..100000 zf = z.to_f y = zf*zf end end x.report("float-square") do for z in 0..100000 zf = z.to_f y = zf**2 end end x.report("int-multip") do zo = 0 for zi in 0..100000 y2 = zo*zo zo += 1 end end x.report("int-multip") do for zi in 0..100000 y2 = zi**2 end end end </code></pre> <p>and this generates the following output:</p> <pre><code>Rehearsal ------------------------------------------------ float-multip 0.125000 0.000000 0.125000 ( 0.125000) float-square 0.125000 0.000000 0.125000 ( 0.125000) int-multip 0.250000 0.000000 0.250000 ( 0.250000) int-multip 0.282000 0.000000 0.282000 ( 0.282000) --------------------------------------- total: 0.782000sec user system total real float-multip 0.110000 0.000000 0.110000 ( 0.110000) float-square 0.125000 0.000000 0.125000 ( 0.125000) int-multip 0.219000 0.016000 0.235000 ( 0.235000) int-multip 0.265000 0.015000 0.280000 ( 0.282000) </code></pre> <p>which clearly shows the the Fixnum multiplication is almost twice as slow as floating point.</p> <p>I have two questions:</p> <ul> <li>Can anyone explain this? A reason I can imagine is that Fixnum multiplication is slower because of the internal checking whether or not it needs to be converted to a Bignum.</li> <li>secondly is there than a quick integer multiplication for ruby?</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload