- microtime: documentation ( source)
- rand: documentation ( source)
<?php
const 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);
(new A())->run();
$et1 = microtime(true) - $time;
print 'Execution time with *property* accessor: ' . $et1 . PHP_EOL;
$time = microtime(true);
(new B())->run();
$et2 = microtime(true) - $time;
print 'Execution time *without* property accessor: ' . $et2 . PHP_EOL;
print 'Ratio: ' . ($et1/$et2) . PHP_EOL;