I'm working with sorting algorithms and wanted to check, just out of curiosity, the old question What is the fastest sorting algorithm?
I made the typical PHP script to get the time an algorithm takes but the result confuses me:
<?PHP
function microtime_float(){
list($useg, $seg) = explode(" ", microtime());
return ((float)$useg + (float)$seg);
}
$tiempo_inicio = microtime_float();
/* inicio del algoritmo de ordenación */
/* aqui va el algoritmo a observar*/
/* Fin del algoritmo de ordenación */
$tiempo_fin = microtime_float();
$tiempo = $tiempo_fin - $tiempo_inicio;
echo "Tiempo empleado: " . ($tiempo_fin - $tiempo_inicio);
?>
I did some tests with different sorting methods with a small sequence of numbers. and the results are too different in each test:
For example: the results of the Quick Sort Method: for the same sequence of 5 numbers was the following:
1 2 3 4 5 Tiempo empleado: 4.8160552978516
1 2 3 4 5 Tiempo empleado: 3.0994415283203
1 2 3 4 5 Tiempo empleado: 9.0122222900391
1 2 3 4 5 Tiempo empleado: 5.5074691772461
etc, etc
As this seemed crazy to me, I tried with a sequence of 9999 random numbers and the result was much more stable since the integer was almost always the same and only the decimals changed,
However, the question arises: Is this method of testing performance in PHP professional? Is there any better? Will we always have to take our algorithms to the extreme to test them?
Here I translate the answer whose link has been provided by @LuiggiMendoza. Some adaptations of the term profiling may be improvable, but I hope it will be useful.
There are (at least) two solutions:
A rather "primitive" one would be to use
microtime(true)
before and after a piece of code, to get the elapsed time during its execution; other answers already said this and gave examples, so I won't say much more.This is a good solution if you intend to compare instructions with each other; like comparing two types of functions, for example (it's better if it's done thousands of times, to make sure you regulate any "disturbers").
It would be something like this to see how long it takes to serialize an array:
It's not perfect, but it works, and it doesn't take long to make.
The other solution, which works quite well if you want to identify which function is taking a lot of time in an entire script, would be to use:
To get the debug files, you have to install and configure Xdebug ; take a look at the Profiling PHP Scripts page of the documentation.
What I usually do is not to activate the profiler by default ( it generates very large files, and it slows everything down ), but use the possibility of sending a parameter called
XDEBUG_PROFILE
as GET data, to activate the profiling only for the page that I need.The part related to profiling in me
php.ini
looks like this:( Read the documentation for more information )
This screenshot is of a C++ program with KcacheGrind: You'll get exactly the same kind of thing with PHP scripts ;-) (With KCacheGrind, I mean ; WinCacheGrind is not as good as KCacheGrind...)
This allows you to get a good view of the time consumptions in your application (and sometimes it definitely helps to locate the function that is slowing everything down ^^)
Note that Xdebug counts the CPU time consumed by PHP ; when PHP expects a response from a Database (for example), it is not working ; but waiting. Then Xdebug will think that the DB query doesn't take long! This should be profiled in SQL server, not PHP, so...
I hope this helps :-)
Have fun !