From: Konrad Meyer Date: 2007-09-24T13:11:41+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. --nextPart2117252.tOTnPM945E Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline 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 =3D 6; y =3D 7 > > >> x * y > > > > i'm thinking: 6 * 7 > > =3D> 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. =20 > > > > 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 >=20 > ruby2c and ruby2cext are hacks that convert Ruby code into C code. >=20 > Pysco is a JIT that works transparently at runtime. >=20 > If you want to code C/C++ then code C/C++ and be done with it. >=20 > If you want to code Ruby then live with the benefits or detriments of > using Ruby including the less than stellar performance. >=20 > 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. >=20 > 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. >=20 > 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. >=20 > 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. >=20 > 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. >=20 > 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. >=20 > 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. >=20 > 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. =2D-=20 Konrad Meyer http://konrad.sobertillnoon.com/ --nextPart2117252.tOTnPM945E Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQBG9zjyCHB0oCiR2cwRAnGOAJ4pF467vbmRbahxAGpCK8qupAmXJgCffyCu YEhR3mcj6leoLFes6dqINBU= =deee -----END PGP SIGNATURE----- --nextPart2117252.tOTnPM945E--