From: William James Date: 2007-09-23T19:00:13+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 22, 5:16 am, William James wrote: > On Sep 20, 5:02 pm, 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 > > If you actually want speed, use a compiled language. > This FreeBasic code is about 160 times as fast and > demonstrates that using Python for any reason > whatsoever is unjustified. > > Use Ruby when ease of programming is paramount; > use something like FreeBasic or LuaJIT when speed > is paramount. Never submit to Python perversion. > > sub scramble( filename as string ) > dim outname as string > dim as integer in_handle, out_handle, i, size > dim bytes(1 to 4096) as byte > > outname = filename & ".scrambled" > in_handle = freefile > open filename for binary access read as in_handle > out_handle = freefile > open outname for binary access write as out_handle > while not eof( in_handle ) > size = seek( in_handle ) > get #in_handle, , bytes() > size = seek( in_handle ) - size > for i = 1 to size > bytes(i) = bytes(i) or &h80 > next > put #out_handle, , bytes(1), size > wend > close > end sub If that prolixity bothered you, look at a version in the J programming language. read =: 1!:1 write =: 1!:2 scramble =: verb define ((128 (23 b.) a. i. read