From: avi@...4.com (Avi Bryant) Date: 2003-09-23T03:51:00+09:00 Subject: Re: Performance: Ruby vs Java "Thomas Sondergaard" wrote in message news:<3f6e50b5$0$48907$edfadb0f@dtext02.news.tele.dk>... > The difference is that ruby is interpreted and has a dynamic nature that > makes it difficult to optimize (or so I understand), while java is jitted > and for this single non-OO example it is probably as fast as C. I've said this before, and I think it's important to say it again: the performance difference between Java and Ruby has *everything* to do with the JIT and *nothing* to do with Ruby's "dynamic nature", or the fact that Java is statically typed. This is easy to demonstrate with Smalltalk, an equally dynamic language that has JITted implementations. My point here isn't to plug Smalltalk (who, me?), but to drive home my point about JIT compilation vs. static typing. On my machine (Athlon 1400), Ruby takes 121.969s to try that algorithm on 400003. VisualWorks Smalltalk takes 2.158 seconds. That's a 60x speedup, without any ugly primitive types or type declarations. Of course, a good interpeter can help a lot too. Squeak Smalltalk, which is a straight bytecode interpreter with no JIT, manages it in 16.147s - still almost an order of magnitude faster than Ruby. By the way, the code I used in Smalltalk was Integer>>isPrime 1 to: self - 1 do: [:i | 1 to: self - 1 do: [:j | i*j = self ifTrue: [^ false]]]. ^ true Although this is idiomatic Smalltalk (in the same way that the Java code presented was idiomatic Java), we can actually make it closer to the Ruby version by creating range objects and iterating over them: Integer>>isPrime (1 to: self - 1) do: [:i | (1 to: self - 1) do: [:j | i*j = self ifTrue: [^ false]]]. ^ true In both Squeak and VisualWorks this roughly doubles the times.