Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: code execution speed and variable name length

by hossman (Prior)
on Jul 30, 2004 at 05:55 UTC ( #378598=note: print w/replies, xml ) Need Help??


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:

  • zentara's benchmark doesn't take into account the fact that building up a big string takes longer then building up a short string .. hence i tried to make sure the only different between the tests was which key was added to the hash.
  • sleepingsquirrel's benchmark reuses two keys over and over (which does test the time spent hashing the keys, and is fair about the time spent building the string) so he doens't consider the added cost of storing lots of big keys. some people don't consider memory utilization a valid thing to consider when doing a Benchmark like this, but if it takes longer to execute access data because the data structure is really huge, that seems like a fair thing to consider.

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


Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2022-12-02 10:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?