PHP 数组的时间/空间复杂度
除了手动计算之外,是否有其他方法或资源可以找到 PHP 中数组实现的时间和空间复杂度?
PHP 中的数组实际上是一个有序映射。映射是将值与键关联起来的类型。该类型针对多种不同用途进行了优化;它可以被视为数组、列表(向量)、哈希表(映射的实现)、字典、集合、堆栈、队列等等。由于数组值可以是其他数组,因此树和多维数组也是可能的。 - php.net
据我所知,它似乎有地图的一般复杂性
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为它的作用类似于哈希表,所以通过键访问元素时将需要
O(1)
时间。如果您循环遍历数组,自然您将有
O(n)
时间。如果你有时间,你可以实际查看 PHP 的实现此处为数组
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
到目前为止,@Mike-Lewis 描述了访问和迭代
遗漏了什么?
Accessing and iterating is describe by @Mike-Lewis so far
Anything missed?
除了 @Mike Lewis 所说的之外,我还要补充一点,PHP 中的一个数组元素至少占用 52 个字节(证明)
In addition to what @Mike Lewis said, I would add, that one array element in PHP occupies minimum of 52 bytes (proof)