Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: '%hash = ()' is slower than 'undef %hash'

by shmem (Chancellor)
on May 18, 2018 at 13:22 UTC ( #1214846=note: print w/replies, xml ) Need Help??


in reply to Re^2: '%hash = ()' is slower than 'undef %hash'
in thread '%hash = ()' is slower than 'undef %hash'

What is the biggest difference we can obtain by using these two?

The difference can be seen with Devel::Peek:

#/usr/bin/perl use Devel::Peek; use feature 'say'; my %assign = ( foo => 1234, bar => 5678, ); my %undef = ( foo => 1234, bar => 5678, ); %assign = (); undef %undef; say "%assign (",\%assign,") after assignment:"; Dump(%assign); say "%undef (",\%undef,") after undef:"; Dump(%undef); __END__ %assign (HASH(0x195f890)) after assignment: SV = PVHV(0x193fe60) at 0x195f890 REFCNT = 1 FLAGS = (PADMY,SHAREKEYS) ARRAY = 0x19a1d70 KEYS = 0 FILL = 0 MAX = 7 %undef (HASH(0x195f860)) after undef: SV = PVHV(0x193fe80) at 0x195f860 REFCNT = 1 FLAGS = (PADMY,SHAREKEYS) ARRAY = 0x0 KEYS = 0 FILL = 0 MAX = 7

The difference lies in the ARRAY element of the above output. By assignment of an empty list the container of the key/value pairs is preserved, whereas undef erases it and sets it to a NULL pointer.

It is obviously cheaper to just wipe the container (and its content) instead of iterating through it and re-arranging its internal structure. - edit - see below, what dave_the_m writes.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2019-04-21 02:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I am most likely to install a new module from CPAN if:
















    Results (110 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!