in reply to Re^9: Finding the size of a nested hash in a HoH
in thread Finding the size of a nested hash in a HoH

Replacing tasklist.exe with ps, your code worked with my machine and it shows while/each is faster and smaller.
1. About Speed
I noticed from your code, I was very unfair to while/each because I lack value assignment with foreach/keys test function. When I change Benchmark test function from
foreach my $k (keys %h){ }
to
foreach my $k (keys %h){ my $v=$h{$k}; }
, benchmark shows while/each becomes faster
case 10000 hash ready ... count=10000 Sat Nov 12 01:13:29 2011 ---------------------------------------- Rate test_each test_keys test_each 108/s -- -28% test_keys 151/s 40% -- ---------------------------------------- Sat Nov 12 01:14:22 2011 case 100000 hash ready ... count=100000 Sat Nov 12 01:14:23 2011 ---------------------------------------- Rate test_keys test_each test_keys 4.63/s -- -23% test_each 6.02/s 30% -- ---------------------------------------- Sat Nov 12 01:15:20 2011 case 1000000 hash ready ... count=1000000 Sat Nov 12 01:15:32 2011 ---------------------------------------- s/iter test_keys test_each test_keys 2.71 -- -30% test_each 1.89 43% -- ---------------------------------------- Sat Nov 12 01:16:35 2011 case 2000000 hash ready ... count=2000000 Sat Nov 12 01:19:32 2011 ---------------------------------------- s/iter test_keys test_each test_keys 5.69 -- -31% test_each 3.91 46% -- ---------------------------------------- Sat Nov 12 01:21:49 2011

2. About memory
With ps command, while/each loop shows very very small memory usage. Memory allocation of "keys" is significant.
$./025-6.pl -M1=keys -M2=pairs -N=1e6 hash built, starting timer Took 3.931463 and 39.000MB extra memory for 1000000 using keys/pairs m +ethod $./025-6.pl -M1=each -M2=pairs -N=1e6 hash built, starting timer Took 2.434757 and 1.000MB extra memory for 1000000 using each/pairs me +thod
It seems I miss something with GTop.

Replies are listed 'Best First'.
Re^11: Finding the size of a nested hash in a HoH
by BrowserUk (Patriarch) on Nov 12, 2011 at 02:46 UTC
    I noticed from your code, I was very unfair to while/each because I lack value assignment with foreach/keys test function. When I change Benchmark test function from
    foreach my $k (keys %h){ }

    Look at the code again carefully. When the command line argument -M2=pairs the code run for the for / keys test is:

    else { if( $M2 eq 'pairs' ) { for my $k ( keys %hash ) { my $v = $hash{ $k }; ++$count; } }

    Ie. This is equivalent to the while / each case for -M2=pairs of:

    if( $M1 eq 'each' ) { if( $M2 eq 'pairs' ) { while( my( $k, $v ) = each %hash ) { ++$count; } }

    To perform the test for both cases where only the keys are accessed, use a command line argument of -M2=keysonly (or any value other than pairs). The code that gets run for two methods in this case are:

    else { while( my $k = each %hash ) { ++$count; } }

    and

    else { for my $k ( keys %hash ) { ++$count; } }

    Again, equivalent code.

    Thanks for confirming my premise.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.