in reply to My scalars swell

Try coercing the numbers to real numbers while storing them.
[0+$1,$2,0+$3,0+$4,0+$5,0+$6]
I've noticed (in perl 5 point something) that this reduces memory consumption by a noticable amount. It won't save your skin, but it will shave a bit off the top.

In the below brief test, the pure numbers version takes up 5656k, the stringified takes up 9560k.

#!/usr/bin/perl use strict; my @foo = (0..100000); foreach my $f (@foo) { # comment/uncomment the next line to change behaviors $f = '' . $f; } sleep(10); exit(0);
-jackdied

Replies are listed 'Best First'.
Re: Re: My scalars swell
by clintp (Curate) on Oct 20, 2001 at 21:52 UTC
    I'd like to alter your test just a little bit to prove a point. Trying to code to the implementation of a language just isn't wise. Observe:
    my @foo = (0..100000); my @t; foreach my $f (@foo) { # comment/uncomment the next lines to change behaviors $f.=""; push(@t, $f+0); } system("ps auxwwww | grep perl"); sleep(10); exit(0);
    The problem is that the memory saved by trying coerce the scalar back to a number to save memory doesn't always work. For example, if the scalar has been used in a string before you don't get the memory back:
    • With "push(@t, $f+0)" only: Process size: 12716
    • With "push(@t, $f."")" only: process size: 15432
    • With "$f.=""; push(@t, $f+0)" : process size 14276
    In this case (I believe) that perl remembers having done an number->string conversion and caches the value to prevent having to do it again (or have I got that backwards..?). At any rate, trying to outsmart the interpreter doesn't always work and starts awful cargo-cult beliefs.
      I agree it is bordering on black magic, but I've used it to good effect to squeeze the last ounces of mem from my computer. Does anyone else do the Dr Dobbs puzzle every month?

      I tried doing a super search and a google search, but found nothing. Is there a pragma or hint that you can pass to perl to tell it you are just using really plain integer scalars?