<?php
$Time = new TimeCounter(); //No ínicio

//Seu código

$Time->Ativar('MySQL');
//QuerySQL
sleep(3);

//Usar quando voltar pro PHP
$Time->Ativar('PHP');
sleep(5);

//Usar quando for logar o MySQL ou algo do tipo...
$Time->Ativar('MySQLLog');
sleep(7);
$Time->Ativar('PHP');


$Time->Exibir_Totais(); //No final para exibir o Tempo

class TimeCounter
{
	protected $TempoInicial;
	protected $TempoInicialParcial;
	protected $RelogioAtual; //Pode ser PHP, MySQL, MySQLLog
	protected $TempoCorrido = array(
									"PHP" => 0,
									"MySQL" => 0,
									"MySQLLog" => 0,
									);
	protected $Contagem = array(
								"PHP" => 0,
								"MySQL" => 0,
								"MySQLLog" => 0,
								);
	public function __construct()
	{
		$this->TempoInicial = microtime(true);
		$this->TempoInicialParcial = $this->TempoInicial;
		$this->RelogioAtual = "PHP"; //Sempre PHP
	}
	protected function ContabilizarTempoCorrido()
	{
		$TempoParcial = microtime(true) - $this->TempoInicialParcial;
		$this->Contagem[$this->RelogioAtual]++;
		$this->TempoCorrido[$this->RelogioAtual] += $TempoParcial;
		$this->TempoInicialParcial = microtime(true);
	}
	public function Ativar($Relogio) //Inicia Contagem PHP
	{
		if ($this->RelogioAtual != $Relogio)
		{
			if (!array_key_exists($Relogio,$this->TempoCorrido)) trigger_error("Relógio Inexistente...", E_USER_ERROR);
			$this->ContabilizarTempoCorrido();
			$this->RelogioAtual = $Relogio;
		}
	}
	protected function FormatarTempo($tempo)
	{
		return number_format($tempo, 3, ',', '.');
	}
	public function Exibir_Totais()
	{
		$this->ContabilizarTempoCorrido();
		
		$PHP = $this->FormatarTempo($this->TempoCorrido["PHP"]);
		$MySQL = $this->FormatarTempo($this->TempoCorrido["MySQL"]);
		$MySQLLog = $this->FormatarTempo($this->TempoCorrido["MySQLLog"]);
		
		$MySQL_Count = $this->Contagem["MySQL"];
		$Total = $this->FormatarTempo(array_sum($this->TempoCorrido));

		printf('<p class="TempoExecucao">Tempo de execução >>> PHP: %ss - SQL: %ss em %s Comandos - Log: %ss - Total: %ss</p>',$PHP,$MySQL,$MySQL_Count,$MySQLLog,$Total);
	}
	
}
?>