Вопрос

I am testing my php code to try and see which coding methods result in quickest execution. I want to eventually test all of my php code which needs testing to try and create the most rapidly-executed code that I can. I am starting with a simple example on my home page, using microtime() to record the time before an INSERT statement executes and the time after it executes, and then echoing the difference as follows:

//Lots of code
$microtime1 = microtime();
$sql1=("INSERT INTO Table (value1,value2,value3,value4,value5,value6,value7) VALUES
('$value1','$value2', '$value3', '$value4', '$value5', '$value6', 'value7' )");

if (mysqli_query($sql1)) {
    $microtime2 = microtime();
    $Difference = $microtime2 - $microtime1;
    echo "<SCRIPT>
        alert('$Difference');
        location = 'home.php';
        </SCRIPT>";
} else {
    $message = 'The site is having some technical difficulties. Please try again!";
    echo "<SCRIPT>
        alert('$message');
        location = 'home.php';
        </SCRIPT>";
}
//More code

Over 10 trials, I initiated this query by inputting the same 3 letter string ('ddd') into the same input text box and clicking the same button (with no other users logged besides me), recording $Difference each time. I am astonished at how much variance there is in the data. For these 10 trials, the standard deviation in $Difference was 40% of the mean value of $Difference. Is there a better way to do this that I am not aware of? Or will I have to do 10-20 trials for each function to get a usable mean value of $Difference for my other code?

Это было полезно?

Решение

What you are talking about is profiling. Although it is possible to "roll your own", (and you are on the right track in that regard), there are a lot of helpful things out there that will do the heavy lifting for you.

If you want, you can always do what you suggested and smooth out the deviations by looping through multiple iterations of your tests, but if you want comprehensive profiling of all of your PHP I recommend the "xdebug" answer to this question: Simplest way to profile a PHP script

It worked well for me.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top