On Mon, May 12, 2003 at 05:40:04PM +0900, Robert Klemme wrote:
> 
> "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)

Stupid of me, yes. I was thinking C there (init buffer with given capa. :-)
 
> 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:

That was the point of my post (see first example), but in this case you
can get more speed by initializing the string to the given size and then
replacing characters inside with #[]=; this way you don't waste time
realloc()ating the string buffer (which would happen if you use
String#<<).

-- 
 _           _                             
| |__   __ _| |_ ___ _ __ ___   __ _ _ __  
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ 
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
	Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Linux poses a real challenge for those with a taste for late-night
hacking (and/or conversations with God).
	-- Matt Welsh