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:
A couple of things come to mind. You do not specify what Ruby implementation you are using. Since you run Ruby 1.8.6 on Windows, I am going to assume that you are using MRI installed via the Windows One-Click Installer.
This is kind of a worst-case scenario:
Here's a couple of tips that you could try to improve performance:
use a different implementation of Ruby:
-fast
commandline option, which is slightly incompatible with Ruby, but improves performance, including arithmetic performance) and In the latter two cases you might want to revise your benchmarks a bit. Both eventually can compile Ruby code to native machine code, but it might take a while. JRuby for example compiles to JVM bytecode after a method has been executed 20 times and HotSpot Server compiles JVM bytecode to native machine code after it has executed 20000 times. Also, compilation itself takes time, so the program needs to run a while to gain back that cost through improved performance.
In particular, Charles Oliver Nutter, one of the JRuby lead developers, said that depending on the workload, JRuby might take up to 5-15 seconds to ramp up to full speed. Your benchmarks are about 100x too fast (here's a sentence you don't hear every day ...).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With