| |
class_timer.php
<?php
/**
* DS Timer v2.0.0
*
* @name Timer Class
* @version 2.0.0
* @author Narong Rammanee, <ranarong@live.com>
* @copyright Copyright (c) 2010, Narong Rammanee
* @license No Licence Free 2010
*/
class Timer {
private $elapsedTime;
private $unitTime;
/**
* Start Timer
*
* @return void
*/
public function start() {
if(!$this->elapsedTime = $this->getMicrotime())
throw new Exception('Error obtaining start time!');
}
/**
* Stop Timer
*
* @return stop time
*/
public function stop() {
if(!$this->elapsedTime = (round($this->getMicrotime() - $this->elapsedTime, 6) * 1000000))
throw new Exception('Error obtaining stop time!');
$this->unitTime = self::setUnitTime();
return $this->elapsedTime.$this->unitTime;
}
/**
* define private 'getMicrotime()' method
*
* @return micro second time
*/
private function getMicrotime() {
list($useg, $seg) = explode(' ', microtime());
return ((float)$useg + (float)$seg);
}
/**
* define private 'setUnitTime()' method
*
* @return time unit
*/
private function setUnitTime() {
if($this->elapsedTime < 1000) {
$this->unitTime = 'µs';
}
else if($this->elapsedTime >= 1000 && $this->elapsedTime < 1000000) {
$this->elapsedTime /= 1000;
$this->unitTime = 'ms';
}
else if($this->elapsedTime >= 1000000) {
$this->elapsedTime /= 1000000;
$this->unitTime = 's';
}
return $this->unitTime;
}
?>
test_timer.php
<style type="text/css">
body { font:13px Tahoma, Geneva, sans-serif; }
.txt-red { color:red; }
</style>
<?php
require_once('class/class_timer.php');
# assign array $val
$val = range(0, 100);
# assign array $value
$value = range(0, 100);
# create timer instance
$t = new Timer;
# for loop start time
$t->start();
for( $i = 0 ; $i < count($val) ; $i++ )
for ($j = 0; $j < count($value); $j++) { }
# for loop used time
$tt = $t->stop();
# show for loop used time
echo 'for loop used time : <span class="txt-red">'.$tt.'</span><br />';
# foreach start time
$t->start();
foreach ( $val as $name )
foreach ( $value as $name ) { }
# foreach used time
$tt = $t->stop();
# show foreach loop used time
echo 'foreach loop used time : <span class="txt-red">'.$tt.'</span><br />';
?>
เครดิต : Timer class
Demo :- Downlaod
|
|