Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.
Popular code excerpts may be moved to "Code Snippets" by the moderators.
<?php
/*
* This class will calculate time in seconds for
* timing script or function execution times
* Author - scottayy@gmail.com
*/
class timer
{
var $time_start;
var $time_end;
var $round_to = 5;
function start_timer()
{
$this->time_start = $this->microtime_float();
}
function stop_timer()
{
$this->time_end = $this->microtime_float();
}
function calculate_time()
{
return round($this->time_end - $this->time_start, $this->round_to);
}
function microtime_float()
{
if(version_compare('5.0.0',phpversion()))
{
return microtime(1);
} else
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
}
}
?>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
<?php
/**
* Logs the amount of time taken from instantiation to getDuration()
* @package XXXXXX
* @author Chris Corbyn
*/
class PageTimer
{
/**
* The time in microseconds at instantiation
* @var float time
*/
private $start = 0;
/**
* Constructor
*/
public function __construct()
{
$this->start = $this->getMicrotimeAsFloat();
}
/**
* Get the time from $start to now()
* @param int decimal places
* @return float
*/
public function getDuration($round=3)
{
return round($this->getMicrotimeAsFloat() - $this->start, $round);
}
/**
* Process the output of microtime() to get a float
* @return float
*/
private function getMicrotimeAsFloat()
{
$parts = explode(' ', microtime());
return (float) $parts[0] + (float) $parts[1];
}
}
It can also be registered and used for finding bottlenecks.