Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Best way to allocate some memory first

by Fletch (Bishop)
on Dec 02, 2021 at 22:52 UTC ( #11139339=note: print w/replies, xml ) Need Help??


in reply to Best way to allocate some memory first

You could do something like assigning to the last element ($array[39_999] = undef;), but if you're so worried about efficiency that handling 40k items is an issue perhaps perl isn't the best implementation choice. If you gave an Short, Self-Contained, Correct Example that might help with more specific suggestions.

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: Best way to allocate some memory first
by LanX (Sage) on Dec 02, 2021 at 23:04 UTC

      I found a test script from 20 years ago here Re: array pre-allocation trick (top of thread array pre-allocation trick) and tweaked it to add a version explicitly assigning to the last item (with "last" in the name). Machine is of course faster so the rate's much different, but the numbers are maybe similar.

      #!/usr/bin/perl use strict; use warnings; use Benchmark qw /cmpthese timethese/; our $size = 100_000; cmpthese timethese( -10 => { push => 'my @arr; push @arr => $_ for 0 .. $::size - 1', assign => 'my @arr; $arr [$_] = $_ for 0 .. $::size - 1', assignpre => 'my @arr; $#arr = $::size - 1; $arr [$_] = $_ for 0 .. $::size - 1', assignlast => 'my @arr; $arr[ $::size - 1 ] = undef; $arr [$_] = $_ for 0 .. $::size - 1', slice => 'my @arr; @arr [0 .. $::size - 1] = (0 .. $::siz +e - +1)', slicepre => 'my @arr; $#arr = $::size - 1; @arr [0 .. $::size - 1] = (0 .. $::size - ++1)', slicelast => 'my @arr; $arr[ $::size - 1 ] = undef; @arr [0 .. $::size - 1] = (0 .. $::size - ++1)', } => 'none' ); __END__ Rate slicepre slice slicelast assignpre assign assignla +st push slicepre 124/s -- -10% -11% -21% -27% -2 +8% -35% slice 138/s 11% -- -0% -12% -19% -2 +0% -28% slicelast 138/s 12% 0% -- -11% -18% -2 +0% -28% assignpre 156/s 26% 13% 13% -- -8% - +9% -19% assign 169/s 37% 23% 22% 8% -- - +2% -12% assignlast 172/s 39% 25% 24% 10% 2% +-- -10% push 192/s 55% 39% 39% 23% 13% 1 +1% -- ## Same comparisons node 209382 on my machine (maybe bit more apples t +o apples) Rate slicepre slice assignpre assign push slicepre 123/s -- -10% -21% -25% -36% slice 137/s 11% -- -12% -17% -29% assignpre 156/s 27% 14% -- -5% -19% assign 165/s 34% 20% 6% -- -14% push 192/s 56% 40% 23% 17% --

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        Thanks, but honestly I don't plan looking into the details.

        I second what Schwern said in the SO discussion:

        (Only try this if ) You profiled and found a problem.

        It would need MANY "40KB arrays" to make this count on a modern machine.

        Whatever a "40KB array" means, arrays hold refs to other types, it's totally unclear what is meant here.

        This smells like a problem which is best solved with packing into a 40 KB string.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11139339]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2022-05-29 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?