http://qs321.pair.com?node_id=11139360


in reply to Re^2: Best way to allocate some memory first
in thread Best way to allocate some memory first

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.

Replies are listed 'Best First'.
Re^4: Best way to allocate some memory first
by LanX (Sage) on Dec 03, 2021 at 18:38 UTC
    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