------art_146363_31520750.1156579237351
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

On 8/26/06, ako... <akonsu / gmail.com> wrote:
>
> hello,
>
> given an array of strings A, i need to map a given function F to each
> element of A and concatenate the results.
>
> which way is more memory efficient:
>
> 1. A.inject("") { |t,x| t + F(x) }
> 2. (A.map { |x| F(x) }).join
>
> thanks
> konstantin
>
>
>
Hmm I cannot really give you an exact answer but it seems that map/join is
consuming only a third of the objects (but see below for a small
improvement)

Counting objects does not even give you the memory consumption, we do not
know their size.
One could monitor the interpreter of course (sorry lazy day).

However as map/join runs more than 4 times faster I would go for it.
I prefer map/join for it's "functional" elegance too.

Another problem in inject is the "+"  if you use "<<" you get down to the
double of objects and run only 3 times slower

Hope that helps
Cheers
Robert

-- 
Deux choses sont infinies : l'univers et la bóŐise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

------art_146363_31520750.1156579237351--