biohisham has asked for the wisdom of the Perl Monks concerning the following question:

Guys I don't know if this is like a new discovery in here or what, using the join function does actually reduce the size of a particular array, i used Devel::Size to verify this and the results I got were 160 bytes each for @list, @result when
CASE1: my @result = map $_*2, @list;
and
@result = join (',',@result);
a meagre increase in "@result" size was reported-168 bytes, i.e. only 8 bytes when
CASE2: my @result = map $_*55, @list;
and @result = join (',',@result); but when I removed the line
@result = join (',',@result);
I got "@result" size increasing to 290 in the first case and 296 in the second case. Here is my Code, you can change the line 6 to map $_x-> (x=any number) to appreciate this
#!/usr/local/bin/perl use strict; use warnings; use Devel::Size qw(size total_size); my @list = qw(1 2 3 4); my @result = map $_*55, @list; @list = join(',',@list); #@result = join (',',@result); print qq(the size of "@list" is: ).total_size(\@list). " Bytes\n"; print qq(the size of "@result" is: ).total_size(\@result)." Bytes\n";
Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind

Replies are listed 'Best First'.
Re: Memory tip
by jrsimmon (Hermit) on Jun 30, 2009 at 16:14 UTC
    I don't think you understand what you've observed. In your example, you are using join to turn the array (1,2,3,4) into the scalar "1,2,3,4".
      which means the scalar has lesser space than the array, I know join gets into a string different elements but I did not know that there is a difference in the size when we are talking contexts of scalar and lists...
        The scalar also contains less information, and the process is NOT generally reversible.
        It is quite easy to reduce the memory use when your algorithm is allowed to be lossy :)

        For example, consider my @array = (1, 5, "hello,world!", 3.1415);
        There is no way to restore the original array after doing a join ',',@array on it.
        "1,5,hello,world!,3.1415" could have been (1, 5, "hello,world!", 3.1415) or it could have been (1, 5, "hello", "world!", 3.1415).
Re: Memory tip
by ikegami (Patriarch) on Jun 30, 2009 at 19:08 UTC

    You might be interested in:

    $ perl -MDevel::Size=total_size -wle' my @a; print total_size(\@a); @a=(1,2,3,4); print total_size(\@a); @a=(); print total_size(\@a); ' 56 <- Empty 136 <- With 4 numbers 72 <- Empty

    The internal buffer of arrays and strings isn't shrunk when the array or string is shrunk in anticipation that it will grow again.

    Also interesting:

    $ perl -MDevel::Size=total_size -wle' sub foo { my @a; print total_size(\@a); @a=(1,2,3,4); } foo(); foo(); ' 56 <- Fresh variable 72 <- Fresh variable

    Variable aren't even deallocated when they go out of scope!

Re: Memory tip
by cdarke (Prior) on Jun 30, 2009 at 18:19 UTC
    Demonstrates that Perl arrays are not a bit like C arrays. Try it with a hash.