From: Ruby Maniac Date: 2007-09-24T21:50:07+09:00 Subject: Re: I am new to Ruby and I could use some expert advice as to how I can make this code run faster. On Sep 23, 9:11 pm, Konrad Meyer wrote: > Quoth Ruby Maniac: > > > > > > > On Sep 23, 7:55 pm, "Bill Kelly" wrote: > > > From: "Ruby Maniac" > > > > > Okay but wake me up when you can make the original Ruby code I > > > > submitted at the outset of this post run 120 times faster because that > > > > is the difference between Ruby and optimized Python (when Psyco is > > > > used to optimize the Python). > > > > > Whether or not Rubynauts feel like Python is perversion or not it is > > > > amazing easy to achieve machine code speeds from Python as opposed to > > > > the difficulty in getting Ruby code to run even 5x faster once > > > > optimized. > > > > I'm only just now reading about Psyco. But the speed gains > > > described on the introduction page[1] seem about what one would > > > expect if similar techniques were used with Ruby: > > > > "For common code, expect at least a 2x speed-up, more typically 4x. > > > But where Psyco shines is when running algorithmical code ..." > > > > "The difference with the traditional approach to JIT compilers is > > > that Psyco writes several version of the same blocks (a block is a > > > bit of a function), which are optimized by being specialized to > > > some kinds of variables (a "kind" can mean a type, but it is more > > > general)." > > > > So it could compile specialized versions of (portions of) a method > > > that would be called if the arguments are Floats or Fixnums. > > > > I would imagine such a technique could work with Ruby, provided > > > one agrees to disallow various Numeric methods from being > > > redefined. > > > > For example, Ruby allows things like this: > > > > class Fixnum > > > alias_method :old_times, :* > > > def *(other) > > > warn("i'm thinking: #{self} * #{other}") > > > old_times(other) > > > end > > > end > > > > >> x = 6; y = 7 > > > >> x * y > > > > i'm thinking: 6 * 7 > > > => 42 > > > > I'd suppose if one is willing to give up the ability to modify > > > a certain set of Fixnum, Float, and perhaps String methods, one > > > could make the equivalent of Psyco for Ruby. > > > > Indeed, there are a couple of already existing projects in > > > this general category: > > > > http://rubyforge.org/projects/ruby2c/ > > > http://rubyforge.org/projects/ruby2cext/ > > > > [1]http://psyco.sourceforge.net/introduction.html > > > > Regards, > > > > Bill > > > ruby2c and ruby2cext are hacks that convert Ruby code into C code. > > > Pysco is a JIT that works transparently at runtime. > > > If you want to code C/C++ then code C/C++ and be done with it. > > > If you want to code Ruby then live with the benefits or detriments of > > using Ruby including the less than stellar performance. > > > I have coded both Ruby and Python and for me some aspects of Ruby code > > is bass-ackwards and less than intuitive even though Ruby is supposed > > to be an easy to understand langauge that is intuitive, blah-blah- > > blah. For me Python seems far more intuitive than Ruby could ever > > be. > > > Maybe I am just not cool enough to think Ruby is god's gift to > > programming but when I write code I want nothing less than performance > > relative to the amount of time I wish to spend writing code. > > > If I wanted blazing performance I would write Aseembly Language (been > > there and done that back in the day when 8 bits was all there was) but > > one must commit a lot of time to hand-crafting Assembly code and most > > of the time I just don't have that much time to spend writing code. > > > Once upon a time I used to code everything in C but that was well > > before C++ existed as anything other than an idea. And 'yes' I was > > coding some rather intense C code, back in the day, using C++ like > > constructs. > > > Python uses some of the same syntax as Ruby. Python is dynamic like > > Ruby. Python source can be compiled into byte-codes and rolled-up > > into an .EXE for easy delivery to end users. I can code Windows > > services usign Python if I wanted to but I cannot code Windows > > services using Ruby whether I wanted to or not. > > > When I write code that I wish to distribute I would rather not have to > > ship source code. I realize shipping source code is supposed to be > > "cool" but I think it is more "cool" to withhold my source code so my > > customers might be more tempted to buy my object-code than hack-up my > > source code. Also I think it is more "cool" to cause those who wish > > to reverse engineer my code have to spend more time doing so rather > > than simply reading the source code such as is the case with Ruby. > > > Also if someone could code a JIT for Ruby like Psyco then someone > > would have done this already and we wouldn't have to talk about ruby2c > > or ruby2ext. > > > The bottom line is this: "Ruby coders will always have to spend more > > time making their Ruby code run faster than Python coders will have to > > spend making their Python code run faster and Python code will always > > run faster than Ruby code even after YARV has been released to save > > the day until such time as someone codes a JIT for Ruby." <-- Wake me > > up when this statement proves to be false because I will be happy when > > the day comes when Ruby code runs faster than Python code and I don't > > have to ship Ruby source code. > > Yup, a troll. > > Someone go get the troll-repellent charms. > -- > Konrad Meyer http://konrad.sobertillnoon.com/ > > signature.asc > 1KDownload- Hide quoted text - > > - Show quoted text - It is truly sad that people, like me, are labeled as being "trolls" whenever we try to interject some reality into a world where people clearly do not want anything that resembles reality. Why not just admit the fact that Ruby is slow in spite of the fact that Ruby has a compelling syntax with compelling features but it is the slowest langauge aside from JavaScript and once upon a time I loved JavaScript so much I coded everything using it but JavaScript is slow and I never made any attempt to hide that fact.