PHP 数组的时间/空间复杂度 [英] Time/Space complexity of PHP Array

查看:75
本文介绍了PHP 数组的时间/空间复杂度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了手动计算之外,还有其他方法或资源可以在 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?

PHP 中的数组实际上是一个有序映射.映射是一种将值与键相关联的类型.这种类型针对几种不同的用途进行了优化;它可以被视为数组、列表(向量)、哈希表(映射的实现)、字典、集合、堆栈、队列等等.由于数组值可以是其他数组,树和多维数组也是可能的.- php.net

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

推荐答案

因为它就像一个哈希表,所以当你通过一个键访问一个元素时,你将有 O(1) 时间.

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

如果你在遍历数组,自然你会有O(n)时间.

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

如果你有时间,其实可以看看 PHP 的 此处实现数组

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

这篇关于PHP 数组的时间/空间复杂度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆