开发者

What's the time complexity of getting an element in an array in PHP?

I've little idea of how arrays are implemented in PHP, and know that for most OOP languages the complexity is one of constant开发者_JAVA百科 time, O(1), for an array of a predefined type. So what's the deal in PHP with it's dynamic typing, extending arrays, etc.?


Looking at array.c in the PHP source code reveals that they're implemented as hash tables, which means typically O(1) (it's actually O(N) if you're very strict but can be as bad as O(log N)) for looking up an element.

If in doubt, you can always measure though. Create an array of 10, 100, 1000, 10000, 100000, 1000000 etc elements and measure the performance, extrapolate the data to a function and you will have the average performance characteristics.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜