bill lam wrote:
> The relatively poor performance of the ruby program seems to be a result of
> looping writing one byte each time. Can ruby interpreter detect and optimize
> that loop, or you have to reprogram it for a fair comparison.
>
>
> William James wrote:
> > At Mon Sep 24 01:01:12 HKT 2007, Joey K Tuttle wrote:
> >
> >> At 02:53 -0700 2007/09/23, William James wrote:
> >>> Bjorn Helgason wrote:
> >>>
> >>>> 65 66 67{a.
> >>> Thanks. I needed this to convert a simple program that
> >>> was posted on the Ruby newsgroup to J.
> >>>
> >>> Here's a cleaned up Ruby version.
> >>>
> >>> def scramble( fname )
> >>> _fname = fname + ".scrambled"
> >>> data = IO.read( fname )
> >>> File.open(_fname, "wb"){|ff|
> >>> data.each_byte{|c| ff.write((c | 0x80).chr) }
> >>> }
> >>> end
> >>>
> >>> Here's my J version.
> >>>
> >>> read =: 1!:1
> >>> write =: 1!:2
> >>> scramble =: verb define
> >>> ((128 (23 b.) a. i. read <y) { a. ) write <y,'.scrambled'
> >>> )
> >>>
> >>> The J program is about 24 times as fast.
Someone posted a version that was about 7.5 times as fast as
the original version. (It sort of cheats because it doesn't
even do a bitwise OR.)
def scramble( fname )
_fname = fname + ".scrambled"
File.open(fname, "rb") do |f|
File.open(_fname, "wb") do |ff|
ff.write(f.read.tr("\0-\177", "\200-\377"))
end
end
end
Ruby makes programming easy, but it seems to be the slowest
"scripting language".
Go to comp.lang.ruby and look for thread
"I am new to Ruby and I could use some expert advice as to
how I can make this code run faster."
____________________________________________________________________________________
Building a website is a piece of cake. Yahoo! Small Business gives you all the
tools to get online.
http://smallbusiness.yahoo.com/webhosting
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm