Pragmatism in the real world

Quick and Dirty Profiling

Profiling the quick and dirty way works for me a lot of the time. Other times, Zend’s profiler is better, but when I just want to work out which function is taking all the time, the following function peppered throughout my code tells me what’s going on:

function PerfPrint($msg)
{
  global $perf_print_start, $perf_print_delta;
  if(!isset($perf_print_start))
  {
    $perf_print_start = microtime_float();
    $perf_print_delta = $perf_print_start;
  }
  $e = microtime_float();
  $since_start = sprintf("%.04f", ($e-$perf_print_start));
  $delta_time = sprintf("%.04f", ($e-$perf_print_delta));
  $perf_print_delta = microtime_float();
  
  $now = date('H:i:s');
  if(PHP_SAPI == 'cli')
  {
    echo "Perf: $now: since start: $since_start,";
    echo " delta: $delta_time - $msgn";
  }
  else 
  {
    echo "<p>$now: since start: $since_start,";
    echo " delta: $delta_time - $msg</p>";
  }
}