Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time/Space complexity of PHP Array

Is there a way or a resource for finding the time and space complexity of the Array implementation in PHP other than calculating it by hand?

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible. - php.net

From what I can tell it would seem that it has the general complexity of a map

like image 524
kristian Avatar asked Apr 12 '11 20:04

kristian


1 Answers

Because it acts like a hash table, you will have O(1) time when accessing an element by a key.

If you are looping through the array, naturally you will have O(n) time.

If you have time, you can actually check out PHP's implementation of array here

like image 158
Mike Lewis Avatar answered Sep 29 '22 13:09

Mike Lewis