Perl: the Markov chain saw PerlMonks

Comment on

 Need Help??
1. What (if any) storage inefficiency (relative to hashes) is created in perl when using arrays for sparsely indexed data?

It all depends on 'how sparse'":

```\$a[ \$_*2 ] = \$_ for 1 .. 1e5;;
\$h{ \$_*2 } = \$_ for 1 .. 1e5;;

print total_size( \$_ ) for \( @a, %h );;
4497312
7393098

undef( @a ); undef( %h );;
\$a[ \$_*20 ] = \$_ for 1 .. 1e5;;
\$h{ \$_*20 } = \$_ for 1 .. 1e5;;

print total_size( \$_ ) for \( @a, %h );;
19177376
7493098

undef( @a ); undef( %h );;
\$a[ \$_*50 ] = \$_ for 1 .. 1e5;;
\$h{ \$_*50 } = \$_ for 1 .. 1e5;;

print total_size( \$_ ) for \( @a, %h );;
69509024
7526431

The hash size remains static for a given number of entries, regardless of how sparse they are.

At 1 in 2, the array uses a little over half the space of the hash; but by 1 in 20, almost 3 times as much and by 1 in 50 nearly 10 times as much.

And remember, if your integer range starts at 1 billion; the array would require ~12GB (of basically wasted space), to hold the lowest value. Whilst you could wrap that over in an api to subtract 1 billion from each index, the additional subroutine call overhead would completely negate the array's lookup speed advantage; and then some.

2. What (if any) lookup inefficiency (relative to arrays) is created in perl when using hashes for positive integer indexed keys?

Test:

```\$a[ \$_ * 20 ] = \$_ for 1 .. 1e5;;

\$found = 0; say time; defined( \$a[ \$_ ] ) and ++\$found for 1 .. 20e5;
+say time; say \$found;;
1359141040.60138
1359141041.00958
100000

\$h{ \$_ * 20 } = \$_ for 1 .. 1e5;;

\$found = 0; say time; exists( \$h{ \$_ } ) and ++\$found for 1 .. 20e5; s
+ay time; say \$found;;
1359141122.04252
1359141123.06596
100000

The array takes 0.4 seconds to do 2 million lookups of which 5% are found and 95% not.

The hash takes 1.02 seconds to do the same lookups.

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.

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
 [Corion]: Mhrrrr. Some project made changes to a report I ingest. I tell them "the format of the new rows is bad". They change it, without understanding/ knowing what other systems might act on this suffix. While it now works for me, I'm not really happy with ... [Corion]: ... their approach to changing things and waiting who screams at them.

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2017-07-24 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (354 votes). Check out past polls.