- microtime: documentation ( source)
- define: documentation ( source)
- rand: documentation ( source)
<?php
define('N', 1000);
class Redis
{
public function run($n)
{
$a = rand($n, 2*$n);
}
}
class A
{
protected $redis;
function __construct()
{
$this->redis = new Redis();
}
function getRedis()
{
return $this->redis;
}
function run()
{
for ($i = 0; $i < N; $i++) {
$this->getRedis()->run($i);
}
}
}
class B
{
protected $redis;
function __construct()
{
$this->redis = new Redis();
}
function run()
{
for ($i = 0; $i < N; $i++) {
$this->redis->run($i);
}
}
}
$time = microtime(true);
$a = new A();
$a->run();
$et1 = microtime(true) - $time;
print 'Execution time with *property* accessor: ' . $et1 . PHP_EOL;
$time = microtime(true);
$b = new B();
$b->run();
$et2 = microtime(true) - $time;
print 'Execution time *without* property accessor: ' . $et2 . PHP_EOL;
print 'Ratio: ' . ($et1/$et2) . PHP_EOL;