1

http://www.securityandcaffeine.com/2008/04/03/php-mysql-and-mysql_fetch_array/

The above article surprised me. It says:

mysql_fetch_array() - "1 – Use quotes! 1/7th of the time to get $result['2'] compared to $result[2]"

Any idea whether that's true?

6
  • 3
    If it is true, it has nothing to do with mysql_fetch_array(), as the underlying array implementation within PHP handles fetching a value for a given key.
    – nickb
    Commented Jul 5, 2012 at 13:59
  • As the guy says in a comment below the post, its down to the fact the data is held twice, once by index once by key, if you search by index, it has to turn this into a key then search on this.
    – Jon Taylor
    Commented Jul 5, 2012 at 14:04
  • @Jon That's pretty nonsensical too. The data is held twice in the array, yes. The only downside is that this uses more memory. But it does not look up the key "id" if you input 0. These are two separate keys with separate data, and the lookup times for each are in no way connected.
    – deceze
    Commented Jul 5, 2012 at 14:24
  • @deceze from my understanding (and I didn't really explain myself properly before, so it's my own fault), the data is stored only in the key => data relationship, when php looks up the index version it converts the index into a string key, then looks this up. I may be wrong but thats what I understood from the comment below the article and is what the answers below also seem to confirm.
    – Jon Taylor
    Commented Jul 5, 2012 at 14:26
  • 1
    @Jon I don't think PHP has to turn numeric indexes into strings to look them up, and even if it does, that's apparently still faster on average than string lookups: codepad.org/Xy4BqiTw
    – deceze
    Commented Jul 5, 2012 at 14:34

3 Answers 3

4

Based on this quote:

1/7th of the time to get $result['2'] compared to $result[2].

I highly doubt this is accurate without any justification, especially considering the article links to another person's (nonexistent) blog as a reference.

Why? Because internally, PHP uses hashtables for arrays. Kendall Hopkins answer does a great job of explaining the implementation:

The PHP array is a chained hash table (lookup of O(c) and O(n) on key collisions) that allows for int and string keys. It uses 2 different hashing algorithms to fit the two types into the same hash key space. Also each value stored in the hash is linked to the value stored before it and the value stored after (linked list). It also has a temporary pointer which is used to hold the current item so the hash can be iterated.

So, unless there is a benchmark proving that the two separate hashing algorithms for int and string keys suffer from a 7x performance gap, the article is nonsense.

4

This seems to be based entirely on a misunderstanding. See this quote, highlight mine:

1 – You are wasting speed if you reference the values, but don’t use quotes. According to Reinhold Weber’s Blog: #17: $row[’id’] is 7 times faster than $row[id]“. If you’re doing this a lot, and often… ouch.

Yes, $row['id'] is faster than $row[id], because $row[id] is a syntax error, which tries to resolve a constant, then turns it into a string, and throws a notice at the same time. But $row[1] and $row['id'] are both valid and should perform very much identical. If there's a difference between looking up a numeric index and looking up a string index it must be so minimal as to be not worth your time.

The revelation that PHP arrays don't really have positional keys but are all associative is... not really news, and there are no wide-spread speed problems because of this. In fact, this quick benchmark shows that "numeric string" indexes are easily the slowest lookup of all of them: http://codepad.org/aeNJ2u3O

Anyway, you should be using mysql_fetch_assoc or mysql_fetch_row, which gets you a named (string indexes) or an unnamed (numeric indexes) array respectively. Using _fetch_array, which gets you both, is usually unnecessary.

If you want real performance data, benchmark it for your use case.

2
  • Just for completeness, about 5 lines down from that quote is: "1 – Use quotes! 1/7th of the time to get $result['2'] compared to $result[2]. Simple speed fix."
    – nickb
    Commented Jul 5, 2012 at 14:07
  • 1
    @nick Yes, but if that's based on the above quote, it's nonsense.
    – deceze
    Commented Jul 5, 2012 at 14:07
3

Oh boy... PHP really is a disastrous language.

When you pull a MySQL result as a combined associative/normal array, what you're really getting is an associative array, where some of the keys are numerical indices.

Thus, any lookup is a key lookup.

The difference betweeen $result[2] and $result["2"] is that PHP will need to convert the integer 2 into the string "2", in the first instance, so that it can use it as a key for a lookup.

Whether the conversion takes 6 times the length of the lookup, I couldn't say; I haven't done any benchmarks. However, the fact that it is longer is certainly plausible. Whether it's true or not? The onus would be on the original author of the article to back up those claims.

5
  • 2
    I'm going to go out on a limb and say that if it is faster (and I doubt it would be that much faster) then it would only make any noticeable difference for very small arrays. And we are talking about microseconds here, it's only going to be a meaningful amount of time with at least hundreds of thousands of iterations - and if you have that much data you have bigger problems than a microsecond here and there.
    – DaveRandom
    Commented Jul 5, 2012 at 14:08
  • This benchmark seems very contrary to what you're saying: codepad.org/Xy4BqiTw
    – deceze
    Commented Jul 5, 2012 at 14:36
  • @deceze: Your test doesn't do any converting from an integer to a string.
    – Dancrumb
    Commented Jul 5, 2012 at 15:13
  • @deceze: That said, having read your answer, I'm inclined to agree with your analysis. I still stand by my analysis that PHP is a disaster ;o)
    – Dancrumb
    Commented Jul 5, 2012 at 15:15
  • The test is testing $array[123], which you are suggesting causes a conversion, and also $array['123'] and $array['abc'], so all possible cases.
    – deceze
    Commented Jul 5, 2012 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.