Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: An irrational coding choice found

by BrowserUk (Patriarch)
on Mar 21, 2012 at 16:54 UTC ( [id://960816]=note: print w/replies, xml ) Need Help??


in reply to Re^2: An irrational coding choice found
in thread An irrational coding choice found

one nitpick (probably too obvious to mention ...

It's not so much "too obvious" as not really applicable to the OPs question.

I wasn't trying to define the absolute criteria for when to use an array or a hash.

Just clarify the reasoning for trading the habitual use of hashes for arrays where that is otherwise a practical proposition.

In order for that trade to even begin to be a practical proposition, the "natural keys" to the data have to be small, near contiguous integers; or otherwise be simply and quickly transformable into a range of small, near contiguous integers. If that is not the case, no such trade is possible and the OPs question is moot.

You can save time using a hash if you need to access a single element of a hash, instead of searching the array for your elements.

In order to use the lookup abilities of an associative array, you must start with the key and either:

  • be looking for the associated value;
  • checking for the existence of that key.

For the very specific subset of cases where the array-instead-of-hash trade is possible, it would mean that you would be starting with an index into the array. And either looking to retrieve the value at that index; or test if there is any value at that index.

In both cases, if the initial trade is feasible then the lookup in the array will be substantially faster than lookup in the hash.


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.

The start of some sanity?

  • Comment on Re^3: An irrational coding choice found

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 18:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found