What is php's performance on directly accessing array's row with key specified
I have a question regarding array's performance.... how does php handle array keys? I mean if I do something like $my_city = $cities[15];
.... do开发者_JAVA技巧es php directly access the exact row item in $cities
array or does php iterate trough array until it finds the matched row?
and if it access the row directly... is there a difference in performance between an array with 100 rows and array with 100,000 rows?
like in this example
$my_city = $cities[15];
PHP's arrays are implemented as hash tables, so the elements are accessed as directly as possible, without iterating through everything. Read more about the algorithm here: http://en.wikipedia.org/wiki/Hash_table
It access it directly. Behind the scene everything is memory pointers arithmetic. I can hardly believe that array concept or implementation in PHP somehow differs from other languages like C, Java or C#.
It depends on what is inside the array too.
// $my_city is a reference to the variable
$cities[15] = new stdobj();
$my_city = $cities[15];
// $my_city is a copy of the array row
$cities[15] = 'foobar';
$my_city = $cities[15];
May not be a direct answer to you question, but a reference to an object usually uses less memory than a variable copy.
there is always an obvious difference in performance between an array with 100 rows and array with 100,000 rows
精彩评论