Each test is run 1000 times and the execution time is averaged over two different ways of achieving the same results. Execution times are rounded to millionths of a second, and taken using microtime. The server is running Ubuntu 7.10 server with PHP 5.2.3-1 and Apache 2, and will not have anything else open to keep the margin of error as low as possible. To be on the safe side, my margin of error (between doing this for years, and my guesstimate after running countless hours of benchmarks) is about 15% +/-.
Ordered from most performance impact to least performance impact
PDO vs. Native PHP MySQL Select (without where condition)
Native PHP MySQL
0.003249 - 0.442145 seconds (avg 0.222697 seconds)
PDO Abstraction
0.007958 - 0.026665 seconds (avg 0.017311 seconds)Without getting into too many of the intricacies,
PDO is ~90% faster on average for selects (and averaged with all 3 loop types!).
Associative Array Quoting vs. None
No Quotes
0.006722 - 0.009518 seconds (avg 0.008120 seconds)
Quotes
0.003404 - 0.004184 seconds (avg 0.003794 seconds)This is a horrible thing to do in the first place.
Quoting associative array elements is ~74% faster on average.
preg_replace() vs. ereg_replace() vs. str_replace()
str_replace()
0.001449 - 0.001796 seconds (avg 0.001622 seconds)
preg_replace()
0.002752 - 0.003861 seconds (avg 0.003306 seconds)
ereg_replace()
0.002558 - 0.002854 seconds (avg 0.002706 seconds)
str_replace() is ~50% faster on average. Better to replace simpler things not requiring regex with str_replace(), and ereg_replace() will be phased out in php6.
PDO vs. Native PHP MySQL (with where condition)
Native PHP MySQL
0.225475 - 1.021669 seconds (avg 0.623572 seconds)
PDO Abstraction
0.175147 - 0.462859 seconds (avg 0.319003 seconds)Once again, PDO is noticeably
~48% faster on average, less a few intricacies. Native PHP MySQL seems to lag a little bit as the requests stack up.
Static vs. Non Static Methods
Static Method
0.001223 - 0.001494 seconds (avg 0.001358 seconds)
Non-Static Method
0.002424 - 0.002839 seconds (avg 0.002631 seconds)Static Methods are
without a doubt faster, ~48% on average.
Error Suppression vs. None
Error Suppression
0.008674 - 0.011197 seconds (avg 0.009935 seconds)
No Error Suppression
0.004963 - 0.008407 seconds (avg 0.006685 seconds)
Error suppression ( @function() ) is sssslllloooowwww. ~32% improvement on average.
Unset vs. None
No Unset
0.000993 - 0.001760 seconds (avg 0.001376 seconds)
Unset
0.000949 - 0.001011 seconds (avg 0.000980 seconds)Freeing up memory with unset as you're done with vars.
~28% improvement on average.
PDO vs. Native PHP MySQL inserting
Native PHP MySQL
0.885488 - 1.675858 seconds (avg 1.280673 seconds)
PDO Abstraction
0.567447 - 1.329165 seconds (avg 0.948306 seconds)PDO trounces Native PHP MySQL again, about
~25% better on average.
Split() vs. Explode()
split()
0.001813 - 0.002271 seconds (avg 0.002042 seconds)
explode()
0.001678 - 0.003626 seconds (avg 0.002652 seconds)Split can take regular expressions as delimiters, and runs faster too.
~23% on average.
echo ''; vs. echo '', '';
Echo Per Line
0.009819 - 0.017965 seconds (avg 0.013892 seconds)
Echo Multiple Args
0.009607 - 0.012264 seconds (avg 0.010935 seconds)The numbers agree;
~21% improvement on average.
While() vs. For() vs. Foreach()
while
0.000306 - 0.000313 seconds (avg 0.000309 seconds)
for
0.000321 - 0.000340 seconds (avg 0.000330 seconds)
foreach
0.000330 - 0.000344 seconds (avg 0.000337 seconds)The difference is
negligible (~13% maximum.) As long as the condition doesn't recursively call a function, it's more of a situational choice.
Extra time taken from recursive function calls
while
0.000422 - 0.000441 seconds (avg 0.000431 seconds)
for
0.000323 - 0.000352 seconds (avg 0.000337 seconds)
~5% and 28% slower on average than without recursive calling.
is_numeric() vs. ctype_digit()
ctype_digit()
0.002009 - 0.002462 seconds (avg 0.002235 seconds)
is_numeric()
0.001864 - 0.002074 seconds (avg 0.001969 seconds)
is_numeric() is faster by ~11%.
in_array() vs. iteration
in_array()
0.000987 - 0.001431 seconds (avg 0.001209 seconds)
Iteration
0.001245 - 0.001414 seconds (avg 0.001329 seconds)
Negligible, just 9% without doing too much in an iteration.
Echo vs. Print
Echo
0.000929 - 0.001255 seconds (avg 0.001092 seconds)
0.000980 - 0.001396 seconds (avg 0.001188 seconds)
Negligible difference (~8%), but echo comes out to be a little quicker overall.
Incrementing undefined variable vs. defined
Undefined
0.000191 - 0.000197 seconds (avg 0.000194 seconds)
Defined
0.000179 - 0.000185 seconds (avg 0.000182 seconds)
Negligible. ~6%
Numeric vs. Associative Foreach
Numeric foreach
0.001542 - 0.001718 seconds (avg 0.001630 seconds)
Associative foreach
0.001599 - 0.001727 seconds (avg 0.001663 seconds)Completely
negligible. ~1%
" vs. '
'
0.000813 - 0.000974 seconds (avg 0.000893 seconds)
"
0.000856 - 0.000939 seconds (avg 0.000897 seconds)Completely
negligible. ~1%
Require_once vs. Include_once()
include_once()
0.013582 - 0.015804 seconds (avg 0.014693 seconds)
require_once()
0.013545 - 0.015840 seconds (avg 0.014692 seconds)Completely negligible. ~1% Is there something I completely missed? Disagree with me? Let me know, I encourage you to let me know what you do to optimize.