Benchmarking
Benchmarking measures how long a block of code takes. CodeIgniter's timer service lets you start and stop named timers and read the elapsed time.
Time a block of work
$timer = \Config\Services::timer();
$timer->start('report');
$rows = $this->buildReport();
$timer->stop('report');
$elapsed = $timer->getElapsedTime('report');
log_message('info', 'Report built in {seconds}s', ['seconds' => $elapsed]);
Several timers
$timer->start('db');
$products = $productModel->findAll();
$timer->stop('db');
$timer->start('render');
$html = view('catalog/list', ['products' => $products]);
$timer->stop('render');
Interpreting results
- Measure on realistic data, not a table with ten rows.
- Compare before and after a change instead of chasing absolute numbers.
- Separate database time from rendering time; they need different fixes.
- Repeat a measurement and look at the trend, not a single noisy run.
Timers also appear in the development toolbar, so most day-to-day profiling needs no extra code.
Benchmark before optimising. A change that feels faster is not evidence; a measurement is.