O'Reilly logo

PHP 5 Power Programming by Derick Rethans, Stig Sæther Bakken, Andi Gutmans

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required