Tag Archive: php


How to measure execution time in PHP

While working with José ABCD (that’s not his last name… he likes to remain unknown) we were talking about two ways of solving a problem un PHP and which would be the best in terms of speed. To solve this, we decided to use microtime(). This is what we did:
Code


          $cicles = 5000; // Number of cicles

          $totalTime = 0; // Total time, set to 0
          for ($i=0; $i<$cicles; $i++){ // This for gets run for $cicles times
               $timeInit = microtime(); // Initial time
                    ## código a probar aquí ## // This is the code to test, it can be a function or several lines of code
               $timeEnd = microtime(); // End time
                    // Next we process time to get a total amout of time during all $cicles
               $timeInit = explode(" ", $timeInit);
               $timeInit = (double) $timeInit[0] + $timeInit[1];
               $timeEnd = explode(" ", $timeEnd);
               $timeEnd = (double) $timeEnd[0] + $timeEnd[1];
               $totalTime += ($timeEnd - $timeInit);
          }
          $timeAvg = $totalTime / $cicles; // Average time
                    // We print our results
          echo "<div>";
          echo "Total = ".$totalTime."<br>";
          echo "Avg = ".$timeAvg."<br>--<br>";
          echo "</div>";

Con José ABCD (no es su apellido real… le gusta el anonimato) estábamos conversando sobre dos formas de solucionar un problema en PHP y cuál sería más eficiente. Para resolver el problema, decidimos usar microtime(). Lo que hicimos a continuación con un ejemplo:
Code


          $cicles = 5000; // Cantidad de ciclos

          $totalTime = 0; // Tiempo total, inicialmente 0
          for ($i=0; $i<$cicles; $i++){ // Este for es ejecutado $cicles veces
               $timeInit = microtime(); // Tiempo inicial
                    ## código a probar aquí ## // Este es el código a probar, puede ser una función o varias líneas de código
               $timeEnd = microtime(); // Tiempo final
                    // Se procesa el tiempo para obtener el total durante todos los ¢cicles
               $timeInit = explode(" ", $timeInit);
               $timeInit = (double) $timeInit[0] + $timeInit[1];
               $timeEnd = explode(" ", $timeEnd);
               $timeEnd = (double) $timeEnd[0] + $timeEnd[1];
               $totalTime += ($timeEnd - $timeInit);
          }
          $timeAvg = $totalTime / $cicles; // Average time
                    // Se muestran los resultados
          echo "<div>";
          echo "Total = ".$totalTime."<br>";
          echo "Avg = ".$timeAvg."<br>--<br>";
          echo "</div>";

I was creating a system and i needed to find a way to assign a work schedule to several employees of a company and control them easily, rapidly and intuitively. For this, from a table created with divs and checkbox, I put “days” on the columns side and “hours” on the row side, then I includes the possibility to select and unselect the work hours of one or more days, clicking and dragging the mouse inside the table. This table is configurable, fast and easy to integrate and use, I gave it the name of EasySelector.

You can see the result in the demo below: Demo
View Full Article »

Estaba creando un sistema y necesitaba encontrar la manera de asignar horarios de trabajo para las personas de una empresa y controlarlos de manera fácil, rápida e intuitiva. Para esto, a partir de una tabla creada con divs y checkbox, coloqué “días” como columnas y “horas” como filas e integré la posibilidad de seleccionar y deseleccionar las horas de trabajo de uno o más días, clickeando y arrastrando el mouse dentro de la tabla. Esta tabla es configurable, rápida, fácil de integrar y utilizar y le dí el nombre de EasySelector.

Pueden ver el resultado en la demo a continuación: Demo
View Full Article »

Powered by WordPress and Motion by 85ideas.