From: Frank Mitchell Date: 2003-09-22T11:08:19+09:00 Subject: Re: Performance: Ruby vs Java Lalit Pant wrote: > > I'm a newcomer to Ruby, and thought I would write a little > brute-force-search program to see how Ruby compares to Java - > performance wise. The results surprised me (I was hoping Ruby would > fare a lot better than it did!). On my WinXP box (AthlonXP 1800+, > 768Meg RAM), the Ruby program takes 1min 50secs to complete, while a > similar java program takes 0.6 secs. [programs snipped] > Any ideas on why i'm seeing such a big difference in performance? File this explanation under "duh", but an obvious big reason for the difference would be the difference in runtimes. Java's HotSpot (at some point) compiles Java bytecodes into native code, so at some point your Java program will be compiled to the equivalent of a C program. On the other hand, Ruby, as I understand it, executes a program by walking over its parse tree, in the classic Gang-of-Four Interpreter pattern. Even compared to pure bytecode interpreters, tree-walking tends to be inefficient, especially for nested loops running over a large number of integers. (A bytecode interpreter is planned, but apparently still just a twinkle in Matz's eye.) For raw number-crunching, you probably wouldn't want to use Ruby (or Python, or Perl, or Tcl). Ruby and similar languages *can* match Java speeds in other tasks, notably when the script spends most of its time using builtin objects implemented in C. You may be interested in a comparison of C, C++, Java, Perl, Python, Rexx, and Tcl: Obviously it doesn't refer directly to Ruby, but many observations from the latter four languages apply to Ruby. -- Frank Mitchell (frankm each bayarea period net) Please avoid sending me Word or PowerPoint attachments. See http://www.fsf.org/philosophy/no-word-attachments.html