14.9. Optimizing Code

This section covers techniques for finding miscellaneous optimizations, including micro-benchmarks, rewriting PHP code in C, and writing procedural versus object-oriented code.

14.9.1. Micro-Benchmarks

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, ...

Get PHP 5 Power Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.