Michiel Pater
Michiel Pater

Reputation: 23023

Comparing execution times in PHP

I would like to compare different PHP code to know which one would be executed faster. I am currently using the following code:

<?php
    $load_time_1 = 0;
    $load_time_2 = 0;
    $load_time_3 = 0;

    for($x = 1;  $x <= 20000; $x++)
    {
        //code 1
        $start_time = microtime(true);

        $i = 1;
        $i++;

        $load_time_1 += (microtime(true) - $start_time);

        //code 2
        $start_time = microtime(true);

        $i = 1;
        $i++;

        $load_time_2 += (microtime(true) - $start_time);

        //code 3
        $start_time = microtime(true);

        $i = 1;
        $i++;

        $load_time_3 += (microtime(true) - $start_time);
    }

    echo $load_time_1;
    echo '<br />';
    echo $load_time_2;
    echo '<br />';
    echo $load_time_3;
?>


I have executed the script several times.

The first result is

0.44057559967041
0.43392467498779
0.43600964546204

The second result is

0.50447297096252
0.48595094680786
0.49943733215332

The third result is

0.5283739566803
0.55247902870178
0.55091571807861


The result looks okay, but the problem is, that every time I execute this code the result is different. Also, I am comparing three times the same code and on the same machine.

Why would there be a difference in speed while comparing? And is there a way to compare execution times and to see the the real difference?

Upvotes: 1

Views: 1341

Answers (4)

RobertPitt
RobertPitt

Reputation: 57268

the reason the results vary is because there are other things going on at the same time, such as windows or linux based tasks, other processes, you will never get an exact result, you best of running the code over a 100 iterations and then devide the result to find the average time take, and use that as your figure/

Also it would be beneficial for you to create a class that can handle this for you, this way you can use it all the time without having to write the code every time:

try something like this (untested):

class CodeBench
{
    private $benches = array();

    public function __construct(){}

    public function begin($name)
    {
        if(!isset($this->benches[$name]))
        {
            $this->benches[$name] = array();
        }

        $this->benches[$name]['start'] = array(
            'microtime' => microtime(true)
            /* Other information*/
        );
    }

    public function end($name)
    {
        if(!isset($this->benches[$name]))
        {
            throw new Exception("You must first declare a benchmark for " . $name);
        }

        $this->benches[$name]['end'] = array(
            'microtime' => microtime()
            /* Other information*/
        );
    }

    public function calculate($name)
    {
        if(!isset($this->benches[$name]))
        {
            throw new Exception("You must first declare a benchmark for " . $name);
        }

        if(!isset($this->benches[$name]['end']))
        {
            throw new Exception("You must first call an end call for " . $name);
        }

        return ($this->benches[$name]['end'] - $this->benches[$name]['start']) . 'ms'
    }
}

And then use like so:

$CB = new CodeBench();

$CB->start("bench_1");

    //Do work:

$CB->end("bench_1");

$CB->start("bench_2");

    //Do work:

$CB->end("bench_2");

echo "First benchmark had taken: " . $CB->calculate("bench_1");
echo "Second benchmark had taken: " . $CB->calculate("bench_2");

Upvotes: 1

Riimu
Riimu

Reputation: 1437

Why would there be a difference in speed while comparing?

There are two reasons for this, which are both related to how things that are out of your control are handled by the PHP and the Operating system.

Firstly, the computer processor can only do certain amount of operations at any given time. The Operating system is basically responsible of handling the multitasking to divide these available cycles to your applications. Since these cycles aren't given at a constant rate, small speed variations are to be expected even with identical PHP commands, because of how processor cycles are allocated.

Secondly, a bigger cause to time variations are the background operations of PHP. There are many things that are completely hidden to the user, like memory allocation, garbage collection and handling various name spaces for variables and the like. These operations also take computer cycles and they can be run at unexpected times during your script. If garbage collection is performed during the first incrementation, but not the second, it causes the first operation to take longer than the second. Sometimes, because of garbage collection, the order in which the tests are performed can also impact the execution time.

Speed testing can be a bit tricky, because unrelated factors (like other applications running in the background) can skew the results of your test. Generally small speed differences are hard to tell between scripts, but when a speed test is run enough number of times, the real results can be seen. For example, if one script is constantly faster than another, it usually points out to that script being more efficient in terms of processing speed.

Upvotes: 1

Shafique Arnab
Shafique Arnab

Reputation: 39

Computing speeds are never 100% set in stone. PHP is a server-side script, and thus depending on the computing power available to the server, it can take a varying amount of time.

Since you're subtracting from the start time with each step, it is expected that load time 3 will be greater than 2 which will be greater than 1.

Upvotes: -1

Your Common Sense
Your Common Sense

Reputation: 157828

there is a thing called Observational error.
As long as your numbers do not exceed it, all your measurements are just waste of time.

The only proper way of doing measurements is called profiling and stands for measuring significant parts of the code, not senseless ones.

Upvotes: 1

Related Questions