From: Robert Klemme Date: 2003-05-12T18:40:06+09:00 Subject: Re: efficiency advice needed "Mauricio Fern�ndez" schrieb im Newsbeitrag news:20030512092410.GA22416@student.ei.uni-stuttgart.de... > > > 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. :-) Don't worry, such things happen to all of us all the time. :-) > > 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#<<). That's true. I just wanted to point out that "<<" is better for cases where you can't do preallocation. Maybe I should've read the beginning of your post more carefully and written my point more clearly. Regards robert