From: "Mauricio Fernández" Date: 2003-05-12T16:10:04+09:00 Subject: Re: efficiency advice needed On Mon, May 12, 2003 at 08:31:58AM +0900, Ryan Pavlik wrote: > On Mon, 12 May 2003 06:56:27 +0900 > "meinrad.recheis" wrote: > > > > data = '' > > (width*height).times{ > > data += rgb.read( 3) + alpha.read( 1) > > } += strikes as extremely inefficient, as it is creating new increasingly big String objects on each iteration!! Ruby will spend quite some time just duplicating the string (ie copying data) and then GC'ing, in fact that'd be O(N^2). Although nearly as bad (making heavy use of realloc), using #<< should give a significant speedup, and you cannot get it much easier than s/+=/<150-fold speedup from your original script :-) You might consider using Inline for this stuff, that'd be pretty neat. -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com I did this 'cause Linux gives me a woody. It doesn't generate revenue. -- Dave '-ddt->` Taylor, announcing DOOM for Linux