PHP Optimization – Using A Timer To Benchmark Code And Increase Speed

By  on  

One of the best parts about being a programmer is that there's seemingly always a better way to do things. A simple code tweak can drastically improve the execution time of your web application. The faster your application executes the quicker you release precious server resources.

The first step in optimizing your existing PHP-based web application for speed is knowing which portions of your website need to be improved. Do your MySQL queries take too long? Are you using efficient functions? Are you using unnecessary server resources?

In my web applications I use a timer class to benchmark how long queries, functions, and entire pages are taking to complete.

The Function

class timer {
	var $start;
	var $pause_time;

	/*  start the timer  */
	function timer($start = 0) {
		if($start) { $this->start(); }
	}

	/*  start the timer  */
	function start() {
		$this->start = $this->get_time();
		$this->pause_time = 0;
	}

	/*  pause the timer  */
	function pause() {
		$this->pause_time = $this->get_time();
	}

	/*  unpause the timer  */
	function unpause() {
		$this->start += ($this->get_time() - $this->pause_time);
		$this->pause_time = 0;
	}

	/*  get the current timer value  */
	function get($decimals = 8) {
		return round(($this->get_time() - $this->start),$decimals);
	}

	/*  format the time in seconds  */
	function get_time() {
		list($usec,$sec) = explode(' ', microtime());
		return ((float)$usec + (float)$sec);
	}
}

The Usage

$timer = new timer(1); // constructor starts the timer, so no need to do it ourselves
/*

 ... mysql query ...

*/
$query_time = $timer->get();
/*

 ... page processing ...

*/
$processing_time = $timer->get();

The Output

Once you know which areas of your website need improvement, making your code more efficient can be easy if you're code is well-commented and organized. Use the timer -- which areas of your website did you notice needed improvement and how did you optimize the code? I'd love to hear your problems and solutions!

Recent Features

  • By
    5 Awesome New Mozilla Technologies You’ve Never Heard Of

    My trip to Mozilla Summit 2013 was incredible.  I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out.  MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

Incredible Demos

  • By
    Fading Links Using jQuery:  dwFadingLinks

    UPDATE: The jQuery website was down today which caused some issues with my example. I've made everything local and now the example works. Earlier this week, I posted a MooTools script that faded links to and from a color during the mouseover and mouseout events.

  • By
    JavaScript Canvas Image Conversion

    At last week's Mozilla WebDev Offsite, we all spent half of the last day hacking on our future Mozilla Marketplace app. One mobile app that recently got a lot of attention was Instagram, which sold to Facebook for the bat shit crazy price of one...

Discussion

  1. Clyde

    Hi!
    I’m new to php and I’m making a php code that will search for operating system, browser, ip address used by a user who log on my web server which is stored in a mysql db.

    I’m using three queries that will display distinct operating system, browser and ip address used by the user.

    So my problem is that my code loads real slow, it takes more than a minute to display the results.

    How do I make this load faster?

  2. Thanks for this usefull class! I never thought about my codes performance before.

  3. Hi, I’m currently working on a project which requires some code optimization, the codes was written by someone else, I do need to optimize asap. The queries take s forever to fetch about 5500 records in mysql using php script:

    Thanks for your prompt response.

  4. Aparna Dey

    Hi, I have been searching for a benchmarking tool for PHP which would enable me to do a performance test after tweaking the php.ini file or make any other optimisation to my code.But, I have not come across anything in particular. Can anybody help???

    Kindly reply.

  5. This is a nice one! Useful for optimizing the page execution.

  6. As usual, your article is great. I don’t always find your articles during my google search but when I see one in the list of results I always click on it because you have great content!

  7. littleguy

    Clean and simple timer class. Thanks!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!