From: Mohit Sindhwani Date: 2007-09-24T12:08:38+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. Ruby Maniac wrote: > On Sep 22, 12:23 am, William James wrote: > >> Ruby Maniac wrote: >> >>> I am new to Ruby and I could use some expert advice as to how I can >>> make this code run faster. >>> >>> def scramble(fname) >>> f = File.new(fname, "rb") >>> _fname = fname + ".scrambled" >>> begin >>> File.exist?(_fname) if File.delete(_fname) >>> rescue >>> end >>> ff = File.new(_fname, "wb+") >>> for l in f >>> l.each_byte{|c| ff.write((c | 0x80).chr) } >>> end >>> f.close() >>> ff.close() >>> end >>> >> About 2.8 times as fast. >> >> def scramble( fname ) >> _fname = fname + ".scrambled" >> mask = 0x8080808080808080 >> data = File.open( fname, "rb" ){|f| f.read } >> pad = (8 - (data.size % 8)) % 8 >> data << "p" * pad >> File.open( _fname, "wb"){|ff| >> array = data.unpack('Q*') >> array.map!{|x| x | mask } >> ff.write( array.pack('Q*')[0 ... -pad] ) >> } >> end- Hide quoted text - >> >> - Show quoted text - >> > > 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 still confused about why someone wouldn't just have a C extension for a Ruby module instead of going through all this claimed hard work. We know the facts: a VM solution is better than an interpreted language; a compiled solution is better than a VM. Yes, we get it - Python, with the effort you put in, is faster. But, it's simply a case of matching problems to languages. I don't think anyone on this group is saying that Ruby is the answer for everything. There are known issues and limitations to Ruby - some of these will be resolved and some will not. As I mentioned before (I think in this thread), Ruby serves its purpose in many situations. You would not write the firmware of a mobile phone in Ruby - nor would you in Python - nor would you in Java. You would most likely write it in C/C++ and where needed, a bit of assembly. We can discuss this endlessly. Why not write our web applications in assembly? It is shown that Java takes more resources than C++ and I saw a paper that showed that on average, C++ /may/ be quite a bit slower than C and people claim assembly is about 10x faster than C in extreme cases. So, let's just write everything in assembly language and get a speedup of 2000 times or some absurd number like that. Match languages to the problems and the resources. You can, with a C extension, easily get the performance you talk about. The point is that for anything complex, it is very likely that Ruby will let you get to the point where you know where the bottlenecks are much quicker. Cheers, Mohit. 9/24/2007 | 11:08 AM.