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.
The version I wrote in FreeBasic is 9 times as fast as J.

Interesting - what did your FreeBasic version look like?

It's very verbose; the language is designed to be quite
compatible with Microsoft's QBasic, although it has pointers and
C-style macros.  The executable is 27,648 bytes. (I hope we won't
be tarred and feathered for being so off topic.)


' The comment character (') is almost invisible.
sub scramble( filename as string )
  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.
end sub

dim t as double
t = timer
scramble "words"
print timer - t



____________________________________________________________________________________
Be a better Globetrotter. Get better travel answers from someone who knows. 
Yahoo! Answers - Check it out.
http://answers.yahoo.com/dir/?link=list&sid=396545469
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm



--
regards,
bill
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to