8.25. Profiling Code
Problem
You have a block of code and you want to profile it to see how long each statement takes to execute.
Solution
Use the PEAR Benchmark module:
require 'Benchmark/Timer.php'; $timer =& new Benchmark_Timer(true); $timer->start(); // some setup code here $timer->setMarker('setup'); // some more code executed here $timer->setMarker('middle'); // even yet still more code here $timer->setmarker('done'); // and a last bit of code here $timer->stop(); $timer->display();
Discussion
Calling setMarker( )
records the time. The
display( )
method prints out a list of markers,
the time they were set, and the elapsed time from the previous
marker:
------------------------------------------------------------- marker time index ex time perct ------------------------------------------------------------- Start 1029433375.42507400 - 0.00% ------------------------------------------------------------- setup 1029433375.42554800 0.00047397613525391 29.77% ------------------------------------------------------------- middle 1029433375.42568700 0.00013899803161621 8.73% ------------------------------------------------------------- done 1029433375.42582000 0.00013303756713867 8.36% ------------------------------------------------------------- Stop 1029433375.42666600 0.00084602832794189 53.14% ------------------------------------------------------------- total - 0.0015920400619507 100.00% -------------------------------------------------------------
The Benchmark module also includes ...
Get PHP Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.