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

Hi forks, Here is my question: The string size is around 20-100K. I timed the process time, and assignment takes around 1-2 seconds. I am surpised at this performance because I expected this operation would take almost nothing under my machine. Any idea about this? Thanks in advance!
my $string = getString($ARGV[]); ENDING MY TIMER sub getString{ my @thisarray = @_; my $thisString; foreach (@thisarray){ $thisString .= $_; } STARTING MY TIMER return $thisString; }

Replies are listed 'Best First'.
Re: it takes 1-2 seconds to return a string from a subrountine
by tommyw (Hermit) on Aug 14, 2002 at 13:50 UTC

    I've just set this up on my laptop (Celeron 1.3Ghz, 128M ram), and I've got to take the string up to 32 Meg before I start seeing any appreciable effect.

    Using the simple expedient of print time, "\n"; for the timer statements, it does confirm that the return is usually starting and finishing within the same second (there's the occasional second discrepancy).

    Possible suggestions:

    • The string is far bigger than you expect
    • You're running out of memory
    • Your timing code is off
    • You're counting the returns across the lifetime of the process, and it's happening more than once
    • You could use references instead (which avoids having to clone the string), although this sidesteps the question of why it's misbehaving

    --
    Tommy
    Too stupid to live.
    Too stubborn to die.

      The problem is because I have two heavy processes for loading storable data inside the subrountine. After I moved those two out from the subrountine. The problem brothered me is gone. I guess that it is related to system memory swrap and memory allocation . I am sorry for not providing enough information earlier. Thanks a lot!
      use Storable( retrieve, store) my $string = getString($ARGV[]); ENDING MY TIMER sub getString{ my ($file1, $file2) = @_; my @thisarray = @{retrieve($file1)}; # size ~ 20M my %thishash = %{retrieve($file2)}; # size ~ 5M my $thisString; # here I am doing something # to create a $thisString and its size is less than 100K STARTING MY TIMER return $thisString; }
        You may save alot of memory (and perhaps time) if you can avoid dereferencing the large array and hash refs. Don't dereference and assign them to the array and hash; just leave them as references.
Re: it takes 1-2 seconds to return a string from a subrountine
by flounder99 (Friar) on Aug 14, 2002 at 13:33 UTC
    Please show your timer code. There may be something wrong there.

    --

    flounder

Re: it takes 1-2 seconds to return a string from a subrountine
by RMGir (Prior) on Aug 14, 2002 at 15:09 UTC
    I'm not sure about your return time problem, but that routine will build the string much faster if you just do
    sub getString { return join "",@_; }
    You're copying the array and iterating over it in perl, and both could kill you if your array has a large number of elements, even if each element is small.
    --
    Mike
Re: it takes 1-2 seconds to return a string from a subrountine
by spurperl (Priest) on Aug 14, 2002 at 15:05 UTC
    Another wild guess... Could it be that your counter is local to the subroutine, and outside it you see a totally other (perhaps uninitialized) counter ?
    Also, try to run this function only, if it's 1-2 secs indeed you'll feel it. If it's instant (the way it should be on any normal machine), the problem is with your counter.