"Mauricio FernŠŌdez" <batsman.geo / yahoo.com> schrieb im Newsbeitrag
news:20030512071000.GA13884 / student.ei.uni-stuttgart.de...
> You can see that ae.rb's execution time still grows faster than
> linearly... A better solution is
>
> batsman@tux-chan:/tmp$ cat ae2.rb
>
> rgb = File.new "rgbfile", 'rb'
>
> alpha = File.new "alphafile", 'rb'
>
> width = height = ARGV[0].to_i
> data = String.new(" " * 4*width*height)

You don't need the String.new here - this only creates a superfluous copy.
Better do

data = " " * (4*width*height)

If you want to append to a string, using "<<" or "concat" is better than
"+=" since "+=" creates a new instance every time.  You can verify it like
this:

$ cat concat.rb

TIMES = 10000

$st_add = 'x'

$st1 = ""
$st2 = ""

def test1(t)
  t.times { $st1 << $st_add }
end

def test2(t)
  t.times { $st2 += $st_add }
end

test1 TIMES
test2 TIMES

$ ruby -r profile concat.rb
  %   cumulative   self              self     total
 time   seconds   seconds    calls  ms/call  ms/call  name
 42.32     1.64      1.64    10000     0.16     0.19  String#+
 39.38     3.17      1.53        2   763.00  1930.00  Integer#times
 10.99     3.59      0.43    10000     0.04     0.04  String#<<
  6.92     3.86      0.27    10003     0.03     0.03  String#allocate
  0.00     3.86      0.00        2     0.00     0.00  Module#method_added
  0.00     3.86      0.00        1     0.00  3875.00  #toplevel
  0.00     3.86      0.00        1     0.00  1125.00  Object#test1
  0.00     3.86      0.00        1     0.00  2735.00  Object#test2

$

Regards

    robert