Hi there,
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.
So i made the following benchmark, comparing float and integer raising to a square, using multiplication or square ** operand.
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
and this generates the following output:
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)
which clearly shows the the Fixnum multiplication is almost twice as slow as floating point.
I have two questions:
- 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.
- secondly is there than a quick integer multiplication for ruby?