http://qs1969.pair.com?node_id=378598


in reply to code execution speed and variable name length

for what i's worth...

#!/usr/local/bin/perl use strict; use warnings; use Benchmark qw[ cmpthese timethese ]; package Benchmark; my @keys = ('s', 'm' x 10, 'l' x 100, 'h' x 1_000, 'r' x 100_000); sub test { my ($iters, $key) = @_; my %hash = (); # regardless of which key we use, build all the keys so the time # needed to clone a big string doesn't overshadow the time to clon +e # a small string. for (my $i = 0; $i < $iters; $i++) { $hash { (map { $_.$i } @keys)[$key] } = undef; } } my $many = 100; cmpthese(5*$many, { 'short' => sub { test($many, 0) }, 'medium' => sub { test($many, 1) }, 'long' => sub { test($many, 2) }, 'huge' => sub { test($many, 3) }, 'ridiculous' => sub { test($many, 4) }, }); __END__ laptop:~> monk.pl Benchmark: timing 500 iterations of huge, long, medium, ridiculous, sh +ort... laptop:~> monk.pl Benchmark: timing 500 iterations of huge, long, medium, ridiculous, sh +ort... huge: 31 wallclock secs (31.57 usr + 0.00 sys = 31.57 CPU) @ 15 +.84/s (n=500) long: 32 wallclock secs (30.39 usr + 0.03 sys = 30.42 CPU) @ 16 +.44/s (n=500) medium: 33 wallclock secs (30.03 usr + 0.01 sys = 30.04 CPU) @ 16 +.64/s (n=500) ridiculous: 187 wallclock secs (159.85 usr + 18.47 sys = 178.32 CPU) @ + 2.80/s (n=500) short: 30 wallclock secs (30.09 usr + 0.02 sys = 30.11 CPU) @ 16 +.61/s (n=500) Rate ridiculous huge long short medi +um ridiculous 2.80/s -- -82% -83% -83% -8 +3% huge 15.8/s 465% -- -4% -5% - +5% long 16.4/s 486% 4% -- -1% - +1% short 16.6/s 492% 5% 1% -- - +0% medium 16.6/s 494% 5% 1% 0% +--

which seems to indicate that the key size can affect things, but only if you're dealing with seriously huge keys.

Note:

Replies are listed 'Best First'.
Re^2: code execution speed and variable name length
by demerphq (Chancellor) on Jul 30, 2004 at 16:11 UTC

    so he doens't consider the added cost of storing lots of big keys.

    Just wanted to add to this that keys in perl are reused. A key is stored only once, regardless of how many hashes it is used in. So while there is obviously an additional cost of using longer keys it doesnt mean that you will thousands of that key in memory if you have thousands of hashes that have that key within them. This also explains why hash keys are not normal scalar values (SV's).

    I believe that it was Gurusamy Sarathy that did the work whit the intention of making hash based objects more memory efficient.


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi