Hello world. Let us time some code.

In order to use debuglog() all you need to do is include the section of code in your script. Then at critical sections of code add a call to debuglog() passing in a message to yourself. At the end of the script call debuglog_print() and you will be able to trace the timings of different sections of your code. Enjoy.

Download debuglog.inc.php

This script took 0.017583 seconds to complete.


If you are really interested you can see the logs in full:


Deprecated: Implicit conversion from float 1780977650.075542 to int loses precision in /var/www/pgregg.com/source/projects/php/debuglog/debuglog.inc.php on line 69
Script started at: 2026/06/09 04:00:50 0.008 ms: +0.008 ms: Here we are at the top of our script. 0.04 ms: +0.032 ms: Page head has been sent 0.044 ms: +0.004 ms: Calling SQL query: SELECT id FROM sometable WHERE something > 20 (not really, we are faking it) 17.566 ms: +17.522 ms: End of SQL query 17.576 ms: +0.010 ms: Just had a little sleep for 17381 microseconds

Or in HTML format:
Deprecated: Implicit conversion from float 1780977650.075542 to int loses precision in /var/www/pgregg.com/source/projects/php/debuglog/debuglog.inc.php on line 69
Script started at2026/06/09 04:00:50
0.008 ms+0.008 ms Here we are at the top of our script.
0.04 ms+0.032 ms Page head has been sent
0.044 ms+0.004 ms Calling SQL query: SELECT id FROM sometable WHERE something > 20 (not really, we are faking it)
17.566 ms+17.522 ms End of SQL query
17.576 ms+0.010 ms Just had a little sleep for 17381 microseconds


<?php


/*
 * Sample code to demonstrate a cool way yo debug PHP code including timing
 * details.
 *
 * Open Source Code:   If you use this code on your site for public
 * access (i.e. on the Internet) then you must attribute the author and
 * source web site:
 *    http://www.pgregg.com/projects/php/code/code_debug_timing.php
 *    http://www.pgregg.com/projects/php/debuglog/debuglog.inc.phps
 *
 * (c) Paul Gregg, 2007
 * 4th April 2007
 * http://www.pgregg.com/projects/
 *
 */

include '../debuglog/debuglog.inc.php';


// Now here is our regular page contents
// Lets do some stuff and see how long it takes.

debuglog('Here we are at the top of our script.');

header("Content-Type: text/html");
echo <<<EOHEAD
<html>
<head><title>Page debug example</title></head>
EOHEAD;

debuglog('Page head has been sent');

echo '<body><p>Hello world. Let us time some code.</p>';
$sql = 'SELECT id FROM sometable WHERE something > 20';
debuglog('Calling SQL query: ', $sql, ' (not really, we are faking it)');
$delay = rand(1000,100000);
usleep($delay);
debuglog('End of SQL query');

debuglog('Just had a little sleep for ', $delay, ' microseconds');

echo '<p>In order to use debuglog() all you need to do is include the section of code in your script.  Then at critical sections of code add a call to debuglog() passing in a message to yourself. At the end of the script call debuglog_print() and you will be able to trace the timings of different sections of your code.  Enjoy.</p>';

echo '<p><a href="http://www.pgregg.com/projects/php/debuglog/debuglog.inc.phps">Download debuglog.inc.php</a></p>';

echo '<p>This script took ', debuglog_timediff(), ' seconds to complete.</p>';

if ($DO_DEBUG) {
  echo '<hr><p>If you are really interested you can see the logs in full:<pre>';
  debuglog_print();
  echo '</pre>';
  echo '<p>Or in HTML format:';
  echo '<table border="1">';
  debuglog_print(false,'<tr><td>','</td><td>','</td></tr>');
  echo '</table></p>';
}

echo '<hr>';

highlight_file(__FILE__);
?>