Ruby Performance vs. Java

R

Robert Bazinet

------=_Part_24892_3396696.1131050724894
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I am new to this list and Ruby in general. I had read a blog post recently
that concerned me a bit,
http://www.pankaj-k.net/archives/2005/11/ruby_or_java_a.html.

The post points out two apps written to do the same task the same way, one
in Java and the other in Ruby. The Java application turned out to be 12x
faster than the Ruby app. I know we are not really comparing apples to
apples here but I was wondering if there are any real-world test results ou=
t
there that show Ruby's performance vs. other languages and where Ruby stack=
s
up.

Thanks,
Rob Bazinet

------=_Part_24892_3396696.1131050724894--
 
R

Ryan Leavengood

I am new to this list and Ruby in general. I had read a blog post recentl= y
that concerned me a bit,
http://www.pankaj-k.net/archives/2005/11/ruby_or_java_a.html.

The post points out two apps written to do the same task the same way, on= e
in Java and the other in Ruby. The Java application turned out to be 12x
faster than the Ruby app. I know we are not really comparing apples to
apples here but I was wondering if there are any real-world test results = out
there that show Ruby's performance vs. other languages and where Ruby sta= cks
up.

This topic comes up every few months on this list, and you might find
it interesting to search the archives at either ruby-talk.org or
comp.lang.ruby on Google Groups.

But to sum things up:

Yes Java is quite a bit faster than Ruby. In fact other scripting
languages like Perl and Python are also faster. Most of this is due to
the fact that Ruby does not in fact have a VM, as Mr. Kumar suggests.
It actually evaluates an AST (Abstract Syntax Tree, the result of
parsing) directly. There is currently work being done to create a VM
for Ruby, called YARV (http://www.atdot.net/yarv/.) This will make
Ruby much faster.

Still even when Ruby gets a VM Java will probably always be faster,
for a few reasons:
1. Java is a statically typed language, which allows for many more
optimizations that are harder or impossible in a dynamically typed
language like Ruby.
2. Java has many man-years and millions of dollars invested in making
it fast. Ruby does not have a huge corporation with thousands of
programmers behind it. Despite that Ruby does pretty darn good, and in
fact Ruby is probably faster than Java was back in the early days.

Also, I would suspect that Mr. Kumar's Ruby code could benefit from
some tweaking by the experts on this list. Recent threads have shown
that smarter algorithms can make Ruby code much, much faster. In my
opinion his run against his log files seems absurdly slow.

Also when it comes to web development, runtime performance of the
language itself has less of an impact, as network latency and in
particular, database access, are usually the bottlenecks.

Regards,
Ryan
 
M

MenTaLguY

I am new to this list and Ruby in general. I had read a blog post recently
that concerned me a bit,
http://www.pankaj-k.net/archives/2005/11/ruby_or_java_a.html.

The post points out two apps written to do the same task the same way, one
in Java and the other in Ruby. The Java application turned out to be 12x
faster than the Ruby app. I know we are not really comparing apples to
apples here but I was wondering if there are any real-world test results out
there that show Ruby's performance vs. other languages and where Ruby stacks
up.

In most cases, the same algorithm implemented in Java and in Ruby is
going to be faster/more memory-efficient in Java, at least with the
current Ruby implementation (there is no "Ruby VM" as the author
supposes; the current interpreter just walks an AST).

However, Ruby can often make more efficient algorithms easier to write
and use (hence the performance benefits that have been observed with
Rails).

If speed is your most important criterion, for which all others must be
sacrificed, Ruby is (probably) not the right language; but usually there
are other factors, like programmer time -- especially for personal
projects!. In those cases, the analysis that needs to be made is
whether a language is fast enough for your specific task.

If anyone offers you a universally applicable "performance metric", it
is an illusion.

-mental
 
J

JB Eriksson

------=_Part_18298_26865382.1131054054751
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

OTOH, if speed really was the issue, one should go with ASM or something. M=
y
grasp on the ruby philosophy isn't just to write the most efficient code,
but to have fun while writing it.
/Johan

s

In most cases, the same algorithm implemented in Java and in Ruby is
going to be faster/more memory-efficient in Java, at least with the
current Ruby implementation (there is no "Ruby VM" as the author
supposes; the current interpreter just walks an AST).

However, Ruby can often make more efficient algorithms easier to write
and use (hence the performance benefits that have been observed with
Rails).

If speed is your most important criterion, for which all others must be
sacrificed, Ruby is (probably) not the right language; but usually there
are other factors, like programmer time -- especially for personal
projects!. In those cases, the analysis that needs to be made is
whether a language is fast enough for your specific task.

If anyone offers you a universally applicable "performance metric", it
is an illusion.

-mental

------=_Part_18298_26865382.1131054054751--
 
E

Eero Saynatkari

Robert said:
I am new to this list and Ruby in general. I had read a blog post recently
that concerned me a bit,
http://www.pankaj-k.net/archives/2005/11/ruby_or_java_a.html.

The post points out two apps written to do the same task the same way, one
in Java and the other in Ruby. The Java application turned out to be 12x
faster than the Ruby app. I know we are not really comparing apples to
apples here but I was wondering if there are any real-world test results out
there that show Ruby's performance vs. other languages and where Ruby stacks
up.

I could not say what the comparable gains in Java would be but that ruby
code could use some algorithmic optimization.
Thanks,
Rob Bazinet

E
 
R

Ryan Leavengood

Also, I would suspect that Mr. Kumar's Ruby code could benefit from
some tweaking by the experts on this list. Recent threads have shown
that smarter algorithms can make Ruby code much, much faster. In my
opinion his run against his log files seems absurdly slow.

To respond to myself, it seems the main culprit is Mr. Kumar's
LogEntry class, whose source code can be gotten here:
http://www.pankaj-k.net/archives/ruby_java/logentry.rb

I've done some benchmarking and the biggest CPU hog is the call to
DateTime.strptime. By changing that call to just DateTime.new
(arguably removing some functionality from the class), the runtime
performance of 10,000 iterations of calling LogEntry.new goes from
24.18 seconds to 7.39 seconds, on my laptop (P4 2.8 GHz, 1 GB RAM,
Windows XP, similar to his test system.)

I believe his regular expressions could be improved as well. Anyone up
to the challenge of making this LogEntry class much faster?

Ryan
 
R

Robert Bazinet

------=_Part_25792_8498192.1131054873358
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Ryan,

Thank you and the others that replied to my post. As I said, I am new to
Ruby and am learning it. I have a couple projects I would like to do in Rub=
y
but with his blog post it got me to wondering if there was a huge differenc=
e
in performance.

I would imagine there is always proven ways to do something in any language=
 
P

pat eyler

Ryan,

Thank you and the others that replied to my post. As I said, I am new to
Ruby and am learning it. I have a couple projects I would like to do in R= uby
but with his blog post it got me to wondering if there was a huge differe= nce
in performance.

I would imagine there is always proven ways to do something in any langua= ge.
I did see the YARV project in poking around the web for Ruby information,
but this looks like it won't be implemented for a while in the mainstream
Ruby release.

Another option that you could pursue is writing your code in Ruby,
profiling it, and
rewriting bottlenecks in C with RubyInline (if you can't get enough of
a performance
boost out of algorithmic changes).

zenspider has shown some very good results through following the
profile-benchmark-optimize-rewrite in C model.
 
H

Harpo

MenTaLguY said:
If speed is your most important criterion, for which all others must
be sacrificed, Ruby is (probably) not the right language;

Yes, in this case C is probably the good language because it allows you
to write Ruby C extensions.
If anyone offers you a universally applicable "performance metric", it
is an illusion.

Yes, this illusion only point out the fact that 'performance' is, by
itself, an illusion when it is not related to some goal or issue which
may be illusive too.

Hugh !
 
R

Robert Bazinet

------=_Part_28818_18292345.1131073688500
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I would like to see this LogEntry class be rewritten and sent to the author
to Mr. Kumar and rerun the tests to see what optimized code in Ruby can do
against his Java code.

I plan to do my next couple projects in Ruby and use these as a learning
tool for Ruby. I think with the addition of a VM it should make some nice
strides.

Thanks for all that responded.

-Rob Bazinet


To respond to myself, it seems the main culprit is Mr. Kumar's
LogEntry class, whose source code can be gotten here:
http://www.pankaj-k.net/archives/ruby_java/logentry.rb

I've done some benchmarking and the biggest CPU hog is the call to
DateTime.strptime. By changing that call to just DateTime.new
(arguably removing some functionality from the class), the runtime
performance of 10,000 iterations of calling LogEntry.new goes from
24.18 seconds to 7.39 seconds, on my laptop (P4 2.8 GHz, 1 GB RAM,
Windows XP, similar to his test system.)

I believe his regular expressions could be improved as well. Anyone up
to the challenge of making this LogEntry class much faster?

Ryan

------=_Part_28818_18292345.1131073688500--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top