Ars Informatica
September 28, 2020
Health Care Informatics
Web-based MySQL/PHP Databasing
Web Development
Favourite Software
Hardware for the Frugal Fanatic
Graphic Design and Image Processing
Free Scripts and Software
About Us
Contact Us

 Article Feed for this site

PHP Script Timer finds the code that's choking you ...

May 17, 2006

Code choking your server's CPU? Site visitors clicking away because your page is taking too long to respond? Do you know?

PHP typically executes very quickly, though some code just plods. Retrieving and parsing remote files, image manipulations, multiple complex SQL queries, can grind down your site. Use PHP Script Timer to identify your bottlenecks.

For example, you want to retrieve and parse a remote RSS news feed. Do you use a locally cached file copy if it's current enough? Do you not bother, and just use the remote feed each time? Does it matter?

Using my walRuSS.php RSS feed script, and a remote feed:

Script execution over 10 loops averages 1.55419661999 seconds
Fastest: 1.07788205147 seconds
Slowest: 3.7918651104 seconds

Now, exact same script, exact same feed, but cached:

Script execution over 10 loops averages 0.0444237470627 seconds
Fastest: 0.0355970859528 seconds
Slowest: 0.0772120952606 seconds

Surprised? I was. And I use a local cache file rather than the remote feed, if the cache file is 'fresh' enough.

PHP Script Timer is short, simple, and very easy to use. First, reference the script:

include 'PHP_script_timer.php';

Next, call the timing script immediately before the code you wish to time:

$timing_loops = 1;
for ($current_loop = 0; $current_loop < $timing_loops; $current_loop++) {
$time_start = microtime_float();

And at the end of the code you're timing:

$timing[] = microtime_float() - $time_start;

To run more the timing loop more then once, and calculate fastest, slowest and average execution times, simply specify 2 or more $timing_loops.

Customize your output further by changing the report_timing <STYLE> tag, or alter the HTML output code as you see fit.


This PHP script is released under the terms of the GNU General Public License, i.e. free for you to use, modify, and even redistribute under the terms of this license - see for further details.

magnifier iconview PHP Script Timer source code

download icondownload -
PHP Script Timer code and documentation

Copyright © 2020 Ars Informatica. All Rights Reserved.