简体   繁体   English

PHP 函数 strlen() 的算法复杂度

[英]Algorithmic complexity of PHP function strlen()

Recently I was asked this question on interview and I didn't know how to answer it.最近面试被问到这个问题,不知道怎么回答。

Can anyone answer this question and describe it?任何人都可以回答这个问题并描述它吗?

O(1) since the length is stored as an attribute: source O(1) 因为长度存储为属性:

However, this trivia is worth countering with a discussion about micro-optimising theatre, as kindly provided by our hosts here and here ;然而,这个琐事值得用关于微优化剧院的讨论来反驳,正如我们的主持人在这里这里提供的一样 read those two links and you'll find a good talking point to change the momentum of the conversation next time similar questions come up, regardless of whether you know the particular answer!阅读这两个链接,无论您是否知道特定的答案,下次出现类似问题时,您都会找到一个很好的话题来改变对话的势头!

How the interviewer reacts to your tangent will tell you a lot about how much you want to work with them..面试官对你的切线的反应会告诉你很多关于你想和他们一起工作的程度。

我假设该函数是 O(n),因为它需要遍历字符串一次。

php 7 source code see here php 7源代码请参见此处

O(1) access attribute ZSTR_LEN(s) see here O(1)访问属性ZSTR_LEN 参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM