From: William James Date: 2007-09-24T20:15:08+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, 3: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- Hide quoted text - > > > > - Show quoted text - > > Okay I downloaded Free BASIC and installed it but your code does not > work ! > > On the one hand, FreeBASIC is certainly worth looking into but on the > other hand - there ain't no debugger than I know of unless you know of > a debugger for FreeBASIC. Did it compile without errors? Did you make sure that no lines of the source were broken by your newsgroup program? (If using google, click on "More options" and "Show original".) I didn't include code to invoke the subroutine; I left that up to you. Debuggers are crutches; I never use them. Here's a complete program. Change the filename that is read by the program. function scramble( filename as string ) as double dim as integer in_handle, out_handle, i, size dim bytes(1 to 4096) as byte in_handle = freefile open filename for binary access read as in_handle out_handle = freefile open filename & ".scrambled" 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 '-- Caution. Closes all open files. return 0.0 end function print "! "; print -timer + scramble("testjunk.csv") + timer