Hey you all I got a time calculation question. I'm attempting to do a lot of PHP and AJAX code appending to HTML. I want to know if my code below will accurately calculate the time of entire code being executed or solely just the PHP code. If it just calculates the PHP, what other code can I add to give me an accurate length of time it takes for the code to execute.

<?php
$before = microtime(true);
/*
    A bunch of PHP Code
*/
?>
<!DOCTYPE html>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
  $(function () {
        /*
            A bunch of Jquery Code
        */
    });
</script> 

<body>
    A Lot of HTML Code
</body>
</html>
<?php
$after = microtime(true);
echo ($after-$before);
?>

Dani AI

Generated

Short answer: the PHP timer in your original snippet measures only server-side processing (the time the PHP script spent generating the response). As pointed out, that finishes long before the browser executes your JavaScript. And ’s tip about DevTools is useful—use the browser tools to see resource and rendering timing on the client.

A practical workflow that covers everything:

  • Record server processing time on the server and expose it (Server-Timing header or embed a value in the page).
  • Instrument client work with the Performance API (marks/measures) to capture DOM/script/runtime costs.
  • Optionally POST the client measurements back to the server for aggregated logging.

Example client-side measurement (use inside your JS where the work happens):

performance.mark('start-work');
// run the DOM/jQuery code you want measured
performance.mark('end-work');
performance.measure('domWork', 'start-work', 'end-work');
const dur = performance.getEntriesByName('domWork')[0].duration;
fetch('/timing-collector.php', {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({clientMs: Math.round(dur)})
});

Expose server processing time so the browser or your logs can show server vs client split. Example (modern PHP):

<?php
$start = hrtime(true);
// ... server work ...
$server_ms = (hrtime(true) - $start) / 1e6;
header("Server-Timing: app;dur={$server_ms}");
?>

Notes and troubleshooting:

  • performance.now() / Performance marks measure client-side durations (includes network + server response only insofar as they affect navigation timing). Use window.onload vs DOMContentLoaded depending on whether you need images/resources included.
  • Use the Performance and Network tabs (and Lighthouse) to drill into where time is spent.
  • Measure AJAX calls explicitly and sample/limit logging to avoid overhead and privacy issues.
  • If running older PHP, fall back to microtime(true); if on modern PHP, hrtime() gives higher resolution.

Recommended Answers

All 2 Replies

The PHP code you posted doesn't measure the execution time of the JavaScript. It just measures the time it took the server to parse the page before sending it to the user and well before the user's browser would even start to process the JavaScript.

I'm sure if you search on "JavaScript measure execution time" that you'll find a lot of good advice and sample code.

Member Avatar for Member #120589

You can also look at the Inspect Element > Network in Chrome browser to give you an idea of how things are working for in practice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.