*All* arrays in PHP are hashes
The only difference between my code and Wade's was how we checked for the existence: in_array vs. isset. And without benchmarking, I don't know enough about the internal implementation to know which is better. Interesting to see it's an order of magnitude difference.
--
Drew |
|
Big difference
Here:
http://brian.moonspo...ay-is-quite-slow/ But it only matter when you are trying to do large lookups - which I am. My lookup tables are about 20,000 entries long, and I have 6 of them. Their key size varies from 2 characters to 50 characters, with the vast majority between 12 and 35 characters. |
|
Multiple orders of magnitude difference
is_set will do a hash calculation bucket lookup, and then possibly a chain walk via a linked list if your keys hash too close together. It's a pretty constant access time, no matter how large your list gets.
If I'm coding in Perl, it is a choice to use it. It takes a bit longer to construct and load a hash list though, so I don't use it by the default if I don't need it, and instead want standard array access (queuing, push, pop, numeric access, etc). I understand that it is not an option in PHP, hashes are simply used. It simply depends on what method of access you use. I ASSUME in_array has to access the first item, compare it, access the next item, compare it, read the next item, compare it. But the time you've gotten to the 3rd compare (or so, no I haven't timed this), a hashed lookup would be complete. So how many orders of magnitude is it if is is a 20,000 item lookup table, which would mean (assuming that 3 record walk vs hash access time holds) it is 6,666 times faster. |
|
It's an index vs. value thing.
array_key_exists() and isset() both look at the array's key, which *is* the hash-table lookup. It's wicked fast: probably the fastest thing you can do with an array in PHP.
in_array() looks in the array's *values*. These are *not* in a hash-table and is therefore much much much slower. Wade, who is paid to know this stuff. Q:Is it proper to eat cheeseburgers with your fingers? A:No, the fingers should be eaten separately. |
|
Ahh, but look at my corrected* version
You're checking if the array key exists in the second dimension of the array. I'm checking for the value in the first dimension. Both versions are looking at the same hash.
* I had the parameters reversed until I just now updated it. --
Drew |
|
Why is this an argument?
in_array == bad for large sets.
Dead end on this path. |
|
Because I didn't read all the comments on that thread
Which I now have. So I now understand that the keys are indexed, but the values are not.
--
Drew |
|
Good
I love educational threads.
|
|
Thank you...
Sister Barry Catherine.
Please no rulers today! |
|
I prefer the gentle approach
Like my daughter says to me all the time:
GET OUT OF MY HEAD! I never tell her what to do or think. But little anecdotes and hints are enough to send the wheels spinning. She hates that. |