On Sep 22, 12:23 am, William James <w_a_x_... / yahoo.com> 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.