Tag performance - page 2

Recent Posts

profile.class.php (June 24, 2008)

Every now and then I want to profile a given part of PHP code. For example I want to quickly check wether my current changeset to GeSHi works faster or is horribly slower. For a big change I’ll stick to Xdebug and KCachegrind. But for a quick overview? Overkill in my eyes.

Say hello to profile.class.php, a simple timer class for PHP5 which you can use to get an overview about where how much time is spent. This is in no way a scientific method nor should you take the results of a single run as a basis for you decisions.

I’ve set an emphasize on an easy API so you don’t have to pollute your code with arbitrary hoops and whistles.

UPDATE: You can find the current updated source in the SVN repo of GeSHi.

Simple example

This is a quick example on how you could use the class:

    require 'profile.class.php'; // should be obvious ;-)
    // here might be uninteresting code
    profile::start('overall'); // start a timer and give it a name
                                       // we add this one to get a time
                                       // for the overall runtime
    // this is the code you want to profile
    $foo = count($_SERVER);
    profile::stop(); // stop the last active counter
    $bar = count($GLOBALS);
    profile::stop(); // stop overall timer
    profile::print_results(profile::flush()); // print the results

continue reading...