* Date: 5 Dec 2002 * License: GPL (General Public License) * Purpose: This function will run the line of code * sent as the first parameter and return * the time it took to complete execution. * If a second parameter is passed, the * function will run the code the specified * number of times, taking the average. This * will return a better approximation. The * last parameter allows you to send code to * be executed between iterations, in case * something needs to be reset (ie. global * variables, etc...). The function returns * the number of seconds * with a bunch of decimal places. */ function benchmark($code, $iter = 1, $reset_code = null) { # Determine the overhead in calling the eval function # You might want to to this multiple times to get better # precision. $start = microtime(); eval(""); $end = microtime(); list($start_ms, $start_s) = explode(" ", $start); list($end_ms, $end_s) = explode(" ", $end); $overhead = ($end_ms+$end_s) - ($start_ms+$start_s); # Execute the code the specified number of times for ($i=0; $i<$iter; $i++) { $start = microtime(); eval($code); $end = microtime(); list($start_ms, $start_s) = explode(" ", $start); list($end_ms, $end_s) = explode(" ", $end); $time += ($end_ms+$end_s) - ($start_ms+$start_s); # If reset code was specified, evaluate it if ($reset_code != null) eval($reset_code); } # Return the average speed subtracted by # the overhead of calling eval() return ($time/$iter) - $overhead; } function dateAdd($data, $qtd, $tipo){ //$data aceita d/m/Y H:i:s //$qtd é um signed int //$tipo é uma string q aceita 'dia', 'hora', 'minuto', 'segundo' $tipos['segundo'] = 1; $tipos['minuto'] = 60 * $tipos['segundo']; $tipos['hora'] = 60 * $tipos['minuto']; $tipos['dia'] = 24 * $tipos['hora']; $pos = (strpos($data, ' '))?strpos($data, ' '):strlen($data); $horario = (strpos($data, ' ')!== false)?substr($data, $pos):""; if (!empty($horario)) { $horarioArr = explode(":", trim($horario)); } $horarioArr[0] = (isset($horarioArr[0]))?$horarioArr[0]:0; $horarioArr[1] = (isset($horarioArr[1]))?$horarioArr[1]:0; $horarioArr[2] = (isset($horarioArr[2]))?$horarioArr[2]:0; $dataArr = split('[/-]', substr($data, 0, $pos)); $dataSeg = mktime($horarioArr[0], $horarioArr[1], $horarioArr[2], $dataArr[1], $dataArr[0], $dataArr[2]); $qtd = $qtd * $tipos[$tipo]; $dateAddSeg = $dataSeg + $qtd; return $dateAddSeg; } $code1 = '$dataInicial = "01/08/2006 08:04:20"; date("d/m/Y H:i:s", dateAdd($dataInicial, +15, "dia")); '; $code2 = '$dataInicial = "01/08/2006 08:04:20"; date("d/m/Y H:i:s", strtotime("+15 day",strtotime($dataInicial))); '; echo "
Usando Data textual
"; echo "Executando 100 vezes
"; echo "Benchmark função: ".benchmark($code1, 100); echo "
"; echo "Benchmark strtotime(): ".benchmark($code2, 100); $code3 = '$dataInicial = date("d/m/Y H:i:s","1154573489"); date("d/m/Y H:i:s", dateAdd($dataInicial, +15, "dia")); '; $code4 = '$dataInicial = "1154573489"; date("d/m/Y H:i:s", strtotime("+15 day",$dataInicial)); '; echo "

Usando timestamp
"; echo "Executando 100 vezes
"; echo "Benchmark função: ".benchmark($code3, 100); echo "
"; echo "Benchmark strtotime(): ".benchmark($code4, 100); ?>