PHP: Making faster PHP scripts

February 18th, 2008 by Richy B. Leave a reply »

Making the web has a couple of articles about how to make PHP scripts run faster and more efficiently. Checking the lists show I’m already doing quite a few of the items listed:

Page one

Multiple arguments with echo (i.e. using “,” instead of “.”)
I don’t currently do this, but it’s only 0.396% faster
Reduce function calls (i.e. find out the size of an array before a loop, not in the “for statement”)
I do do this, but not as much as I should – especially with a 54.095% difference!
Avoid using variables if they aren’t needed and unset them once you’ve finished with them
I try to do this, but there doesn’t appear (from the site) to be any speed penalties in not doing this.
Use single quotes (instead of double quotes – ” )
I’ve done this for ages as I knew PHP didn’t have to parse the string – but I didn’t know it was only a 0.696% speed difference.
str_replace() vs ereg_replace() and preg_replace()
I tend to use preg_replace instead of ereg_replace anyway, and I’m aware that using any sort of regular expressions is slow. Therefore I do try and use str_replace instead – after all, it’s a 59.224% saving in speed!


Part 2

Pre-increment is faster than post-increment (i.e. ++$var is faster than $var++)
This I wasn’t aware of and I’ll have to try to start using – with a 21.23% speed up, it’s worth seriously considering.
Avoid regular expressions for input validation
I already try and use “is_int” etcetra instead of regular expressions to validation to avoid speed issues – and using ctype_digit (which I hadn’t heard about before) instead of regular expressions is a 58.98% speed up.
Use explode() instead of split()
If I reminded you that split() supports regular expressions, would you be not-surprised to learn that explode is 34.79% faster?
Use time() rather than date(‘U’)
Considering “date” uses “time” to create a time string – are you in anyway surprised that time() on it’s own is 99.7% faster than passing time() to date(‘U’) to convert from a unixtime to a unixtime?
do loops are faster than while which are faster then for loops
A do-while loop (which I must admit I never use) is 21.81% faster than a “for” loop and just faster then a normal while loop. I’ll have to investigate the do-while loops me thinks.

Part 3

is_numeric() is faster than ctype_digit()
I must admit, I wasn’t even aware of ctype_digit() before now – and with is_numeric being 15.75% faster, I don’t think I’ll give it much investigation.
echo is faster then print
I was aware that echo was slightly faster then print, but I wasn’t aware why (it’s because print returns a status value). It’s only 0.16% faster, but every second counts.
Better conditional statements
If $var2 is more likely to be false than $var1 is, then “if ($var2 && $var1)” is faster then “if ($var1 && $var2)” as the script is less likely to need to do a second conditional comparison. there can be a 1.797% speed up.
=== (identical) is faster then == (equal)
This is one that suprised me – === asks PHP to check that $a and $b are totally identical (even down to have a plain number 123.45 in $a vs having a string “123.45” in $b) and so I would think checking the type of the variable would slow it down. But no, === is 16.32% faster than ==!.
(int) is faster than (intval)
I’ve never heard of intval before now – and with int being 54.45% faster then intval, I think I’ll stick with int!

This post is over 6 months old.

This means that, despite my best intentions, it may no longer be accurate.

This blog holds over 12 years of archived content - during that time, I may have changed my opinion of something, technology will have advanced (and old "best standards" may no longer be the case), my technology "know how" has improved etc etc - it would probably take me a considerable amount of time to update all the archival entries: and defeat the point of keeping them anyway.

Please take these posts for what they are: a brief look into my past, my history, my journey and "caveat emptor".

Comments are closed.

gamy-dance
%d bloggers like this: