From: "John W. Kennedy" Date: 2006-02-24T11:43:33+09:00 Subject: Re: Huge performance gap Alexis Reigel wrote: > Stephen Waits wrote: >> E. Saynatkari wrote: >> >>> Post the code somewhere, there might be room for improvement >>> in the algorithm though it will still be considerably slower. >> >> It looks, to me, like he attached his code to the OP. >> >> Regardless, it doesn't matter. Algorithmic improvements may help both >> the C++ and Ruby versions - but it's not going to change the fact that >> one is a relatively low-level language, compiled to native machine code, >> and the other is an interpreted dynamic language. To compare them is >> either ridiculous, or more likely in this case, simply ignorant. >> >> --Steve >> > Why should that be ridiculous or ignorant? > I stated that I was aware of the differences between interpreted and > compiled languages. But that does not change the fact that I believe > that this does not explain the performance gap. An execution time of > 27.65 seconds against 0.33 seconds is not just nothing is it? It's a > factor of over 80 times. Besides, I implemented the same code in java > too, which isn't native code as well and runs in a virtual machine too, > and it executed in about the same time as c++. Most modern Java implementations (on full computers, not PDAs and the like) are /not/ interpreted. The interpreter compiles the bytecode into machine code. Furthermore, even when interpreted, Java has typed variables. A Java int is always a 32-bit 2's-complement integer. "i = j + k;", where each of i, j, and k is an int, is a simple operation involving about three instructions in either the Java Virtual Machine or the real machine. A Ruby variable could be an integer, a big-integer, a floating-point number, a character string, or even something to which "+" doesn't apply, and, every time an expression is evaluated, that all has to be worked out. The convenience of Ruby, Perl, REXX, JavaScript, and similar languages is considerable. But it comes at a price. If the bottleneck in the program is the speed of your disk, or of your IP connection, that price probably doesn't matter. But if you're doing substantial calculations in RAM, it may not be worth it. You can't always generalize, though. Ruby is faster than Java at finding perfect numbers (probably because Ruby's implementation of big integers is faster than Java's), and both are considerably faster than Perl (probably because Perl forces /all/ numbers to be big integers, if any are) (and GNU Common LISP is faster than Ruby). -- John W. Kennedy "But now is a new thing which is very old-- that the rich make themselves richer and not poorer, which is the true Gospel, for the poor's sake." -- Charles Williams. "Judgement at Chelmsford"