1. sizeof vs count
First up are the sizeof and count functions. They can both be used to count the number of items in an array but does one do it better?
sizeof vs count
sizeof: 3.75928902626 seconds
count: 3.33035206795 seconds
Time saved: 0.428936958313 seconds; 12.8796280262%
The evidence says yes. The count function was over 12% faster in this test. Both functions are fast though taking 3-4 microseconds to count an array with 100,000 items. You might think it isn’t worth it but remember count is also a character shorter. Not only is it faster to run but it is also faster to type!
2. is_int vs is_integer
Next it is time to try some validation with is_int and is_integer.
is_int vs is_integer
is_int: 3.11394786835 seconds
is_integer: 3.42630600929 seconds
Time saved: 0.312358140945 seconds; 9.11646946006%
The difference is smaller this time but again both functions are executing in a little over 3 microseconds. is_int is faster than is_integer so again the shorter function name wins. Is this going to be a pattern?
3. chop vs rtrim
Tidying up a string breaks the pattern. Maybe my analysis was a little premature.
chop vs rtrim
chop: 4.73731994629 seconds
rtrim: 4.41647195816 seconds
Time saved: 0.320847988129 seconds; 7.26480301852%
rtrim is a little faster and these functions take longer at a little over 4 microseconds.
4. doubleval vs floatval
Both these functions allow you to quickly pick a float, or double, out of a string.
doubleval vs floatval
doubleval: 4.56571412086 seconds
floatval: 4.35375285149 seconds
Time saved: 0.211961269379 seconds; 4.8684727087%
4% or 211 nanoseconds is next to nothing. I would want to use floatval but I wouldn’t exactly be kicking myself if I slipped a doubleval into my code instead.
5. fwrite vs fputs
So far all the functions have been running fast anyway so the difference haven’t been that significant but things are starting to get interesting with the run time jumping to 20-24 microseconds to write a short string to a file.
fwrite vs fputs
fwrite: 24.9826359749 seconds
fputs: 20.1990799904 seconds
Time saved: 4.7835559845 seconds; 23.6820488199%
With a difference of over 4 microseconds you could run both fputs and floatval in the time it takes to run fwrite. The difference is over 23% which clearly makes fputs the better function. It’s shorter as well.
6. implode vs join
This one really surprised me. I expected these functions to be faster. Both these functions allow you to convert an array into a string. implode is the opposite of explode and join allows you to join the items in an array with a ‘glue’ string. In this case the array had 100 items.
implode vs join
implode: 47.2712550163 seconds
join: 50.1287050247 seconds
Time saved: 2.85745000839 seconds; 5.70022705949%
5% doesn’t seem like all that much but 2 microseconds shouldn’t be ignored.
7. ini_alter vs ini_set
Finally lets look at modifying the environment. I actually cheated here and just set the max execution time to 60 seconds a million times. I think the result is still valid though
ini_alter vs ini_set
ini_alter: 103.332370043 seconds
ini_set: 86.2883789539 seconds
Time saved: 17.0439910889 seconds; 19.7523598143%
As might be expected these functions are expensive when it comes to execution time. That 17 microsecond saving is massive as well making ini_set the superior choice.
Speedy sites
PHP has a vast array of functions enabling a large variety of tasks to be performed without the programmer ‘reinventing the wheel’. These functions are not all created equal though and real speed boosts can be achieved by choosing the right functions.
v