After some experimentation, I have found the bottleneck to my code and can
now write an RB program that runs *faster* than perl.

The problem is, it doesn't do just what I want.

Consider this perl code:

perl -le 'my $isOdd=1 ; while (<>) { chomp ; if ($isOdd) {print ('\''case
'\'' , $_) ; $isOdd=0} else {print("r = \"" , $_ , "\"\n") ; $isOdd=1} }'
test.txt > perltest.txt

Here is the equivalent RB code:

  #pragma BackgroundTasks false
  #pragma BoundsChecking false
  
  dim fIn, fOut as FolderItem
  dim t as Double = microseconds
  
  fIn = DesktopFolder.Child( "test.txt" )
  fOut = DesktopFolder.Child( "output.txt" )
  
  dim tIn as TextInputStream = fIn.OpenAsTextFile
  dim tOut as TextOutputStream = fOut.CreateTextFile
  
  dim caseStr as string = "case "
  dim varStr as string = "r = """
  dim closingStr as string = """" + chr( 13 )
  
  dim line as string
  dim isOdd as boolean = true
  do until tIn.EOF
    line =  tIn.ReadLine
    'if IsNumeric( line ) then
    if isOdd then
      tOut.Write( caseStr )
      tOut.WriteLine( line )
      isOdd = false
    else
      'line = line.ReplaceAll( """", """""" )
      tOut.Write( varStr )
      tOut.Write( line )
      tOut.WriteLine( closingStr )
      isOdd = true
    end if
  loop
  
  tIn.Close
  tOut.Close
  
  t = microseconds - t
  t = t / 1000000
  
  MsgBox( format( t, "#,0.000" ) + " seconds" )
  
If you look at the commented lines, you will see two of the four differences
from my original code.

First, I use multiple "Write" statements instead of concatenating a string
and writing it once. This alone provided a 7-second speedup.

Second, I assigned the static text to variables that get written out to the
output file. 

Third, rather than testing a line to see if it's entirely a number, I use a
switch to process every other line.

Fourth, I stopped replacing quotes with double-quotes.

What the last two have in common is that they replace RB functions
(IsNumeric, ReplaceAll), and that is where the bottleneck is. Without these
calls, the RB program is slightly faster than the perl version. With either
of these, the RB program slows down, and with both, slows down dramatically.

The bottom line: Function calls slow down RB significantly, but, other than
the simplest applications, there is no way to avoid them. By definition,
until this is "fixed", RB programs will run slower than they can, and
probably slower than other, better optimized, languages.

__________________________________________________________________________
Kem Tekinay                                                 (212) 201-1465
MacTechnologies Consulting                              Fax (914) 242-7294
http://www.mactechnologies.com                        Pager (917) 491-5546

  To join the MacTechnologies Consulting mailing list, send an e-mail to:
           [EMAIL PROTECTED]






_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to