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

Re: Effecicncy of key-only hash

by tilly (Archbishop)
on Aug 24, 2008 at 07:13 UTC ( [id://706502]=note: print w/replies, xml ) Need Help??


in reply to Effecicncy of key-only hash

A string is larger than an integer is larger than undef. Proof:
#! /usr/bin/perl -w use strict; use Devel::Size qw(total_size); my %hash1 = ( shave => '', the => '', modern => '', way => '', ); my %hash2 = ( shave => 1, the => 1, modern => 1, way => 1, ); my %hash3 = ( shave => undef, the => undef, modern => undef, way => undef, ); print "1: " . total_size(\%hash1) . "\n"; print "2: " . total_size(\%hash2) . "\n"; print "3: " . total_size(\%hash3) . "\n"; __END__ 1: 309 2: 261 3: 245
If you want the fastest way to initialize that hash, I believe it is
my %hash; undef(@hash{qw(shave the modern way)});
but your maintenance programmer may have something nasty to say about that.

Replies are listed 'Best First'.
Re^2: Effecicncy of key-only hash
by ysth (Canon) on Aug 24, 2008 at 18:01 UTC
    undef(@hash{qw(shave the modern way)});
    Do @hash{qw(shave the modern way)}=(); instead. It's arguably a bug that the former creates the shave, the, and modern keys if they don't exist. Note that it does not set their values to undef if they do already exist.
      Many years ago there was a discussion on p5p about the fastest way to initialize an empty hash. I forget who it was who brought up that construct as the fastest possible way to do it, but I do remember it was someone who should know. Maybe Nick Ing-Simmons, but I won't swear to it. However at the time it was certainly faster than assigning an empty list because all other versions created temporary intermediate scalars and that one does not.

      Of course now, many versions later, it might not be still true. But that fragment has stuck in my head.

      Please note that I included that version for amusement, and not for serious use. Which I indicated with my comment about the maintenance programmer's response. Which comment has been confirmed by the questions and complaints we've had. :-)

        Since I'm in mod_perl I don't care at all how fast the hash initializes. I only care about the memory footprint, and to a lesser extent the lookup time.
Re^2: Effecicncy of key-only hash
by leonidlm (Pilgrim) on Aug 24, 2008 at 08:06 UTC
    I know it isn't my thread but I had to ask: Can you please explain what you are doing here:
    undef(@hash{qw(shave the modern way)});
    Why you accessing a hash as an array ? ('@' char)? I am missing something I think ...
      The @ tells you that what you expect to return is an array, and the curly braces {...} tell you that what you access is a hash.

      qw(shave the modern way) is a list, so @hash{qw(shave the modern way)} is a list ("slice") of items in %hash with the listed keys.

      It's a hash slice. See perldata for more information on hash and array slices

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-26 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found