PHP:函数"count"的复杂度[即O(1),O(n)]是什么?

编程入门 行业动态 更新时间:2024-10-27 16:35:44
本文介绍了PHP:函数"count"的复杂度[即O(1),O(n)]是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

count() 函数的Big-O时间复杂度是多少?用于数组?

What is the Big-O time complexity of the count() function for arrays?

示例

$x = array(1,2,3); echo count($x); // how many operation does it takes to count the elements // of the array? is it 3, or is it 1

推荐答案

$ time php -r '$a=range(1,1000000); $b=0; for($i=0;$i<10;$i++) $b=count($a);' real 0m0.458s $ time php -r '$a=range(1,1000000); $a=array(1); $b=0; for($i=0;$i<10;$i++) $b=count($a);' real 0m0.457s

对我来说似乎很O(1).

Seems pretty O(1) to me.

经过测试的PHP版本:PHP 5.3.3-1ubuntu9.1 with Suhosin-Patch (cli) (built: Oct 15 2010 14:00:18)

Tested PHP version: PHP 5.3.3-1ubuntu9.1 with Suhosin-Patch (cli) (built: Oct 15 2010 14:00:18)

更多推荐

PHP:函数"count"的复杂度[即O(1),O(n)]是什么?

本文发布于:2023-11-29 01:37:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644776.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复杂度   函数   PHP   count   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!