This section covers techniques for finding miscellaneous optimizations, including micro-benchmarks, rewriting PHP code in C, and writing procedural versus object-oriented code.
Often, you may find yourself wondering which approach is the fastest. For example, which is faster—str_replace() or preg_replace()—for a simple replacement? You can find the answer to many of these questions by writing a little micro-benchmark that measures exactly what you are looking for.
The following example is a library file (ubm.php) to run micro-benchmarks, followed by an example benchmark that tells you which is faster:
<?php register_shutdown_function('micro_benchmark_summary'); $ubm_timing = array(); function micro_benchmark($label, ...