- microtime: documentation ( source)
<?php
class HardcodedLocator {
public function getFoo() { return null; }
}
class ConfigurableLocator {
protected $services = array();
public function set( $name, $value ) {
if ( isset( $this->services[$name] ) ) {
throw new LogicException( 'Cannot redefine!' );
}
$this->services[$name] = $value;
}
public function get( $name ) {
if ( !isset( $this->services[$name] ) ) {
throw new LogicException( 'No such service' );
}
return $this->services[$name]();
}
}
$hl = new HardcodedLocator();
$cl = new ConfigurableLocator();
$cl->set( 'foo', function() { return null; } );
$rounds = 10000;
$htime = microtime( true );
for ( $i = 0; $i < $rounds; $i++ ) {
$hl->getFoo();
}
$htime = microtime( true ) - $htime;
$ctime = microtime( true );
for ( $i = 0; $i < $rounds; $i++ ) {
$cl->get( 'foo' );
}
$ctime = microtime( true ) - $ctime;
echo 'overhead: ' . ( ( $ctime - $htime ) / $rounds );