I'm curious whether searching for a single value in a hash is generally 
(always?) faster than searching for it in a list.  The programming I'm 
doing is in PHP, but I think it's a question with a general answer.

For example, assume I have a list of a million e-mail addresses [1] and 
I want to very quickly check whether a certain address is in that list.  
If I had a one-dimensional array, I could use the PHP function 
in_array() to determine whether I'm in the list:

$address_list = array("[EMAIL PROTECTED]", "[EMAIL PROTECTED]", ...);
if(in_array("[EMAIL PROTECTED]", $address_list)) ...

On the other hand, I could construct a hash with the same data:

$address_list = array("[EMAIL PROTECTED]" => 1, "[EMAIL PROTECTED]" => 1, ...);
if($address_list{"[EMAIL PROTECTED]"}) ...

Since hashes are designed to provide fast lookups of any given key, it 
seems to me that the second approach would yield results more 
efficiently.  But I never took a data structures class, so I may be 
missing something.

Thoughts?

Thanks,
Jeff

[1] No, I'm not a spammer. :)  It's just an example.

Attachment: pgpkSgYNBJxNa.pgp
Description: PGP signature

/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/

Reply via email to