elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.
 
Inicio Ayuda Buscar Ingresar Registrarse
28 Mayo 2012, 13:08  


Tema destacado: Sigue las noticias más importantes de elhacker.net en ttwitter!

+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web
| | |-+  PHP
| | | |-+  Calcular tiempo de ejecución de un script en PHP
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Calcular tiempo de ejecución de un script en PHP  (Leído 801 veces)
madpitbull_99
Moderador Global
***
Desconectado Desconectado

Mensajes: 1.898



Ver Perfil WWW
Calcular tiempo de ejecución de un script en PHP
« en: 15 Abril 2011, 17:29 »

Muchas veces en mis proyectos web tengo que optimizar el tiempo de carga y de ejecución de los scripts. Para eso me he creado una pequeña clase muy fácil de utilizar.

Código
<?php
 
/**
* @author MadPitbull
* @copyright 2011
*/

 
   class PageLoadingTime{
 
       private $time;
       private $initTime;
       private $finTime;
       private $totalTime;
 
       /**
        * PageLoadingTime::__construct()
        * Starts the timer.
        * @return null
        */

       public function __construct() {
           $this->initPageLoadingTime();
       }
 
       private function initPageLoadingTime() {
           $this->time = microtime();
           $this->time = explode(" ", $this->time);
           $this->time = $this->time[1] + $this->time[0];
           $this->initTime = $this->time;
       }
 
       /**
        * PageLoadingTime::getPageLoadingTime()
        * Returns a float var with the page loading time in micro seconds.
        * @return float
        */

       public function getPageLoadingTime() {
           $this->time =  microtime();
           $this->time = explode(" ", $this->time);
           $this->time = $this->time[1] + $this->time[0];
           $this->finTime = $this->time;
           $this->totalTime = ($this->finTime - $this->initTime);
 
           return $this->totalTime;
       }
 
   }
 
?>

Su funcionamiento es muy sencillo, utiliza dos timers. El primero es inicializado al invocar al constructor de la clase y el valor del segundo es capturado invocando el método getPageLoadingTime y luego se guarda en otra variable la resta del tiempo registrado al principio del script con el tiempo registrado al final del script.
Os dejo un ejemplo de como funciona y mas abajo un enlace para descargar la clase y el ejemplo.

Código
<?php
 
   include ("class.PageLoadingTime.php");
   echo "[+] Testing the PageLoadingTime PHP Class <br />";
 
   $timer = new PageLoadingTime();
 
   for ($i = 0; $i <= 100; $i++) {
       echo "<p style='text-indent: 1em'>" . $i . "<p>";
   }
 
   echo "<p>Execution time: <b>" . $timer->getPageLoadingTime() . "</b></p>";
 
?>

El bucle for lo he puesto solo para probar el funcionamiento de la clase.
Os dejo el enlace para descargar la clase, si no queréis descargarla podéis copiarla directamente de aquí, funcionará sin
problemas. [Descargar]


En línea



«Si quieres la paz prepárate para la guerra» Flavius Vegetius

[Taller]Instalación/Configuración y Teoría de Servicios en Red
WHK
吴阿卡
Ex-Staff
*
Desconectado Desconectado

Mensajes: 4.113


The Hacktivism is not a crime


Ver Perfil WWW
Re: Calcular tiempo de ejecución de un script en PHP
« Respuesta #1 en: 17 Abril 2011, 07:25 »

está bueno, me gustó, vee si puedes hacer un benchmark y le adjuntas el uso de memoria con memory_get_usage y esas cosas :D


En línea

Mi foro Ultra Off-Topics: http://whk.drawcoders.com/foro/

Gracias a todos! Good bye!
El As del Club Paris


Desconectado Desconectado

Mensajes: 1.818


Ver Perfil WWW
Re: Calcular tiempo de ejecución de un script en PHP
« Respuesta #2 en: 19 Abril 2011, 18:46 »

Una cosa, por que en lugar de llamar a la funcion initPageLoadingTime desde el contructor, mejor no metes el codigo de dicha fncion dirctamente en el contructor.

Código
public function __construct() {
  $this->time = microtime();
  $this->time = explode(" ", $this->time);
  $this->time = $this->time[1] + $this->time[0];
  $this->initTime = $this->time;
}

Y asi ahorras unos cuantos bytes de memoria  ;D

Saludos

En línea

sudo suck --mycock -o force
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Powered by SMF 1.1.16 | SMF © 2006-2008, Simple Machines