Ruby vs Perl performance


#1

I have downloaded perl and ruby program in net.I run both the
programs.But the Ruby program performance very wost than perl.Can any
one tell me the reasons and
soultion to make ruby as faster than perl.
I have attached perl program.

#!/usr/local/bin/ruby

BAILOUT = 16
MAX_ITERATIONS = 1000

class Mandelbrot

    def initialize
            puts "Rendering"
            for y in -39...39 do
                    puts
                    for x in -39...39 do
                            i = iterate(x/40.0,y/40.0)
                            if (i == 0)
                                    print "*"
                            else
                                    print " "
                            end
                    end
            end
    end

    def iterate(x,y)
            cr = y-0.5
            ci = x
            zi = 0.0  while(1)
                    i += 1
                    temp = zr * zi
                    zr2 = zr * zr
                    zi2 = zi * zi
                    zr = zr2 - zi2 + cr
                    zi = temp + temp + ci
                    return i if (zi2 + zr2 > BAILOUT)
                    return 0 if (i > MAX_ITERATIONS)
            end

    end

end

time = Time.now
Mandelbrot.new
puts
puts “Ruby Elapsed %f” % (Time.now - time)

            zr = 0.0
            i = 0

#2

Can’t be done. Unless you intend to build your own super fast vm.

On my box Perl consistently runs in around 2 seconds, Ruby takes 9.

There is nothing that can be done to the source (other than getting
the code to actually compile - your source is broken) that can improve
more than a second or so on that performance.

Haven’t tried it under JRuby though, you might experience better
performance there.

But to honest I wouldn’t sweat it that Ruby is not the best tool for
this sort of thing (number crunching), my wife’s car cannot best a
dragster but it gets her where she needs to be.


#3

Vetrivel V. wrote:

I have downloaded perl and ruby program in net.I run both the
programs.But the Ruby program performance very wost than perl.Can any
one tell me the reasons and
soultion to make ruby as faster than perl.

Try ruby 1.9?
-=r


#4

On Sat, Feb 7, 2009 at 2:35 PM, Vetrivel V.
removed_email_address@domain.invalid wrote:

I have downloaded perl and ruby program in net.I run both the
programs.But the Ruby program performance very wost than perl.Can any
one tell me the reasons and
soultion to make ruby as faster than perl.
I have attached perl program.

Did you take code form this site:
http://www.timestretch.com/FractalBenchmark.html ?

Try it with ruby1.9.1. I get about 1.90s for perl and 2.80s for
ruby1.9.1 so it’s not bad.

Pozdrawiam

Rados³aw Bu³at
http://radarek.jogger.pl - mój blog


#5

On Sat, Feb 7, 2009 at 3:42 PM, Peter H. removed_email_address@domain.invalid
wrote:

Haven’t tried it under JRuby though, you might experience better performance
there.

JRuby is faster than 1.9 even without warming. ~2s for jruby, ~2.8s
for ruby1.9.1. With warm up it goes down to ~1.2s and with --fast flag
to ~0.75s.


Pozdrawiam

Rados³aw Bu³at
http://radarek.jogger.pl - mój blog


#6

Vetrivel V. wrote:

I have downloaded perl and ruby program in net.I run both the
programs.But the Ruby program performance very wost than perl.Can any
one tell me the reasons and
soultion to make ruby as faster than perl.
I have attached perl program.

You’re not going to make it run faster (not at this time anyway). You
can always likely improve and code more efficiently, but some things
are just going to be faster.


#7

Vetrivel V. wrote:

I have downloaded perl and ruby program in net.I run both the
programs.But the Ruby program performance very wost than perl.
Can any one tell me the reasons and soultion to make ruby as
faster than perl. I have attached perl program.

Ruby and Perl are two totally different programing environments and are
really not to be compared with regards to their run time performance.
With Ruby the performance issue is shifted to the entire project life
cycle, and indeed to the complexity of the project. Mere execution time
of an application represents only a tiny fraction of what is truly
measured when comparing procedural language like Perl and object
oriented Ruby. If you compare the two languages in this respect Perl
doesn’t come even close to where Ruby stands. In fact I believe if you
look at performance issues from this angle, Ruby stands out very proudly
as the best performing programming language of all times.


#8

Vetrivel V. wrote:

MAX_ITERATIONS = 1000

class Mandelbrot

    def initialize
            puts "Rendering"
            for y in -39...39 do
                    puts
                    for x in -39...39 do
                            i = iterate(x/40.0,y/40.0)

Don’t indent so deeply. It makes the code much harder to read.

Don’t write a mandelbrot program in an interpreted language.
Write it in a compiled language like OCaml.


#9

2009/2/7 Vetrivel V. removed_email_address@domain.invalid:

I have downloaded perl and ruby program in net.I run both the

choose the right tool for a job!

If i had a programming task which needs special attention to speed
and/or security i would choose Ada and not Ruby.

On the other hand i have written a Ruby program which uploads pictures
to several stock photo sites. In this program it is almost completely
useless to optimize for a maximum speed of the program itself because
the bottleneck is the uploading of the pictures and some manual
editing of the pictures at the website. In this case i don’t care if
the rotation of a single picture take some seconds more in Ruby, so i
took Ruby because it was more fun to write the program in Ruby.

-Thomas


#10

William J. wrote:

Vetrivel V. wrote:
Don’t indent so deeply. It makes the code much harder to read.

Don’t write a mandelbrot program in an interpreted language.
Write it in a compiled language like OCaml.

OCaml, Ada, C, C#, Perl, French … I think all of these things are
rather misplaced here. Isn’t this about Ruby? The fact is that one needs
to choose a language that best meets their project requirements,
however, it does not seem that Vetrivel was searching for a language,
he was not happy with Ruby performance, full stop! He more likely is
searching for answers in general, and he as also some who responded, do
not quite understand what are the drawbacks and what are the true
benefits of using such a powerful tool as Ruby. Ruby is setting the
standards of contemporary computing. C#, which is in many ways a clone
of Ruby is evidently is a prominent proof of this. Still, nothing comes
close to the simplicity, elegance, power and indeed the overall
performance and primarily the productivity of designers and programmers
that work with Ruby.


#11

William J. wrote:

                            i = iterate(x/40.0,y/40.0)

Don’t indent so deeply. It makes the code much harder to read.

Don’t write a mandelbrot program in an interpreted language.
Write it in a compiled language like OCaml.

OCaml:

let bailout = 16.0
let max_iterations = 1000

let iterate x y =
let cr = y -. 0.5 and
ci = x and
zi = 0.0 and
zr = 0.0 in
let rec iterate2 zi zr i =
if i > max_iterations then
0
else
let temp = zr *. zi and
zr2 = zr *. zr and
zi2 = zi *. zi in
if zi2 +. zr2 > bailout then
i
else
iterate2 (temp +. temp +. ci) (zr2 -. zi2 +. cr) (i + 1)
in
iterate2 zi zr 1

let mandelbrot =
for y = -39 to 38 do
print_endline “”;
for x = -39 to 38 do
let i = iterate
(float_of_int x /. 40.0) (float_of_int y /. 40.0) in
print_string ( if 0 = i then “*” else " " )
done
done

let _ = mandelbrot


#12

William J. wrote:

William J. wrote:

OCaml:

let bailout = 16.0
let max_iterations = 1000

let iterate x y =
let cr = y -. 0.5 and
ci = x and
zi = 0.0 and
As I said, rather misplaced discussion. This is about Ruby, not about
some obscure X-sharps which all to us look like the old spaghetti code
from 40 or so years ago when structured techniques became popular after
Pascal bursted into the scene. As for the indentation, Vetrivel clearly
has just discovered vi and Unix clones, where tab is the most prominent
feature. Almost all modern source code editors today implement
GtkSourceView widget library, which manages indentation much more
efficiently than half a century old tools, newly discovered by M$
computer illiterate folks. Lets stop babbling about various screw-sharps
and concentrate on educating newcomers about Ruby!


#13

Igor P. wrote:

William J. wrote:

Vetrivel V. wrote:
Don’t indent so deeply. It makes the code much harder to read.

Don’t write a mandelbrot program in an interpreted language.
Write it in a compiled language like OCaml.

OCaml, Ada, C, C#, Perl, French … I think all of these things are
rather misplaced here. Isn’t this about Ruby? The fact is that one
needs to choose a language that best meets their

“one” is singular, so it’s “best meets his”.

designers and programmers that work with Ruby.
Don’t preach to the choir. Ruby is my favorite language.
Not even Matz would tell you to use it for mandelbrot programs.


#14

William J. wrote:

Igor P. wrote:

William J. wrote:

Vetrivel V. wrote:
Don’t indent so deeply. It makes the code much harder to read.

Don’t write a mandelbrot program in an interpreted language.
Write it in a compiled language like OCaml.

OCaml, Ada, C, C#, Perl, French … I think all of these things are
rather misplaced here. Isn’t this about Ruby? The fact is that one
needs to choose a language that best meets their

“one” is singular, so it’s “best meets his”.

http://en.wikipedia.org/wiki/Singular_they#Generic_they


#15

Doesn’t ruby 1.9 rub faster?

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/


#16

Rados³aw Bu³at wrote:

http://www.timestretch.com/FractalBenchmark.html ?

Try it with ruby1.9.1. I get about 1.90s for perl and 2.80s for
ruby1.9.1 so it’s not bad.

running on core2 duo gentoo

Ruby Elapsed 0.045477

rthompso@raker ~ $ ruby mand.rb
Rendering

                                    *
                                    *
                                    *
                                    *
                                    *
                                   ***
                                  *****
                                  *****
                                   ***
                                    *
                                *********
                              *************
                             ***************
                          *********************
                          *********************
                           *******************
                           *******************
                           *******************
                           *******************
                         ***********************
                           *******************
                           *******************
                          *********************
                           *******************
                           *******************
                            *****************
                             ***************
                              *************
                                *********
                                    *
                             ***************
                         ***********************
                      * ************************* *
                      *****************************
                   * ******************************* *
                    *********************************
                   ***********************************
                 ***************************************
            *** ***************************************** ***
            *************************************************
             ***********************************************
              *********************************************
              *********************************************
             ***********************************************
             ***********************************************
           ***************************************************
            *************************************************
            *************************************************
           ***************************************************
           ***************************************************
      *    ***************************************************    *
    *****  *************************************************** 

    ****** *************************************************** 

   ******* *************************************************** 

 ***********************************************************************
 ********* *************************************************** 

    ****** *************************************************** 

    *****  *************************************************** 

           ***************************************************
           ***************************************************
           ***************************************************
           ***************************************************
            *************************************************
            *************************************************
           ***************************************************
             ***********************************************
             ***********************************************
               *******************************************
                *****************************************
              *********************************************
             **** ****************** ****************** ****
              ***  ****************   ****************  ***
               *    **************     **************    *
                      ***********       ***********
                      **  *****           *****  **
                       *   *                 *   *

Ruby Elapsed 0.045477


#17

Julian L. wrote:

Doesn’t ruby 1.9 rub faster?

Not for that functionality comparing the two languages.


#18

On Sat, Feb 7, 2009 at 11:35 PM, Vetrivel V.
<removed_email_address@domain.invalid

wrote:

I have downloaded perl and ruby program in net.I run both the
programs.But the Ruby program performance very wost than perl.Can any
one tell me the reasons and
soultion to make ruby as faster than perl.
I have attached perl program.

It’s a tortoise and the hare debate really. As a C programmer i can say
that
ruby is rubbish, and yet when i use it i save countless hours of
debugging
and writing kludge code (When you start doing OO in C it’s time to try
something new). For me it’s the difference between actual productivity
and
abandoning projects because i don’t have time, besides i might as well
chew
up some of the idle cycles my shiny new processor always seems to have.
Oh, and despite what some people might say every language has some
merit.
(except cobol)


#19

2009/2/8 Reid T. removed_email_address@domain.invalid:

I don’t believe you :P. Show us the code. Did you use RubyInline or
something?:slight_smile:


Pozdrawiam

Rados³aw Bu³at
http://radarek.jogger.pl - mój blog


#20

Reid T. wrote:

Ruby Elapsed 0.045477

#!/usr/local/bin/ruby

require ‘rubygems’
require ‘inline’

BAILOUT = 16
MAX_ITERATIONS = 1000

class Mandelbrot

 def initialize
     puts "Rendering"
     for y in -39...39 do
         puts
         for x in -39...39 do
             i = iterate(x/40.0,y/40.0)
             if (i == 0)
                 print "*"
             else
                 print " "
             end
         end
     end
 end

 inline do |builder|
     builder.c "
     int iterate (double x, double y)
     {
         int BAILOUT = 16;
         int MAX_ITERATIONS = 1000;
         double cr = y-0.5;
         double ci = x;
         double zi = 0.0;
         double zr = 0.0;
         double zr2 = 0.0;
         double zi2 = 0.0;
         int i = 0;
         double temp = 0.0;

         while (1)
         {
            i += 1;
            temp = zr * zi;
            zr2 = zr * zr;
            zi2 = zi * zi;
            zr = zr2 - zi2 + cr;
            zi = temp + temp + ci;

            if ( zi2 + zr2 > BAILOUT)
            {
                return i;
            }
            if ( i > MAX_ITERATIONS)
            {
                return 0;
            }
         }
     }"
 end

def iterate(x,y)

cr = y-0.5

ci = x

zi = 0.0 while(1)

i += 1

temp = zr * zi

zr2 = zr * zr

zi2 = zi * zi

zr = zr2 - zi2 + cr

zi = temp + temp + ci

return i if (zi2 + zr2 > BAILOUT)

return 0 if (i > MAX_ITERATIONS)

end

end

time = Time.now
Mandelbrot.new
puts
puts “Ruby Elapsed %f” % (Time.now - time)