开发者

Is using a PHP Function Alias poor performance/Bad practice?

开发者 https://www.devze.com 2022-12-19 01:47 出处:网络
Just out of curiosity, Does the alias of a built-in PHP function take any more power than the regular function itself.

Just out of curiosity,

Does the alias of a built-in PHP function take any more power than the regular function itself.

AKA, does sizeof() take more time than count() to run?

Note to Micro-Optimization Police: Unless there is a HUGE difference, I don't plan on trying to micro-optimize my script. Was just curious.

Note to the 'Just try it yourself' Police: I'm not able to access a PHP environment right now.


It seems that this has also expanded to a 'Best Pratice', as PHP's documentation states that aliases are best not used, and in turn should simply be called via the master function. In this case, that would mean tha开发者_StackOverflow社区t count() should be used over sizeof().


I'm quite sure that they compile to the same byte-code equivalents, so the answer will be no.


It is my understanding that aliases are typically the result of a couple different things. For instance, when name-conventions change, or function-names themselves change. It would be disastrous to eliminate the old convention immediately. Additionally, they are helpful for users who come from a different language background, and would like to use many of the same familiar methods they're already used to.

I don't think there's any additional overhead. But be sure to check the official documentation for which method/function name is encouraged as deprecation is always potentially looming in the uncertain future.

"...there are functions which changed names because of an API cleanup or some other reason and the old names are only kept as aliases for backward compatibility. It is usually a bad idea to use these kind of aliases, as they may be bound to obsolescence or renaming, which will lead to unportable script..."

http://php.net/manual/en/aliases.php


Using http://github.com/gms8994/benchmark/blob/master/php/sizeof_vs_count.php, here's what I came up with. Granted, this is only comparing sizeof and count, but it at least answers a portion of the question

glens@glens-desktop:$ ./sizeof_vs_count.php 
sizeof($globals->x);: 1000000 iterations took 7.44s at 134374.010/s
count($globals->x);: 1000000 iterations took 8.21s at 121806.517/s
glens@glens-desktop:~/scripts/benchmark/php
glens@glens-desktop:$ ./sizeof_vs_count.php 
sizeof($globals->x);: 1000000 iterations took 7.84s at 127475.401/s
count($globals->x);: 1000000 iterations took 7.79s at 128437.659/s
glens@glens-desktop:~/scripts/benchmark/php
glens@glens-desktop:$ ./sizeof_vs_count.php 
sizeof($globals->x);: 1000000 iterations took 7.53s at 132807.066/s
count($globals->x);: 1000000 iterations took 7.49s at 133442.192/s
0

精彩评论

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

关注公众号