views:

163

answers:

3

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 that count() should be used over sizeof().

+1  A: 

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

Jonathan Sampson
I'd like to add: Checking `if(sizeof($arr) > 0)` is a lot more semantic than `if(count($arr) > 0)`
Chacha102
Note also that PHP's documentation for `count()` is far thicker than it is for `sizeof()`. Does this show emphasis on one over the other? Perhaps.
Jonathan Sampson
@Chacha102: It is? I'm fairly sure that sizeof is ambiguous. Is it the size of the variable type, the size of an array, or a file size? Whereas count is always going to mean the number of elements in an array.
R. Bemrose
@R. Bemrose, well, knowing the type of data inside a variable would be up to good variable naming conventions, wouldn't it?
Chacha102
@Jonathan I'm not sure if the documentation is saying it is a bad idea is better to use the old or new aliases? (Given that all function names are simply aliases for its underlying code body)
Chacha102
@Chacha102 I would use that which has the heaviest documentation. In this case, `count()` is heavily documented whereas `sizeof()` simply says "Alias of `count()`."
Jonathan Sampson
@Jonathan I would take that to mean that count() is indeed the preferred function as it does something significant (return some data) whereas sizeof() just calls count()
Unkwntech
@Johnathan, looking at the list, it seems that the new aliases (`sizeof`) is the preferred function, as it says the list is provided for people who want to bring the scripts `up-to-date`
Chacha102
@Chacha102: "the old names are only kept as aliases." PHP Documentation refers to `sizeof()` as the alias, and `count()` as the main convention. Following from that, "It is usually a bad idea to use these kind of aliases" would indicate we ought to use `count()` rather than the alias.
Jonathan Sampson
@Chacha102: It is a little confusing :) I'll admit that much.
Jonathan Sampson
+2  A: 

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

troelskn
+1  A: 

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
gms8994
AKA, they are probably inconclusive.
Chacha102