Skip to content

Instantly share code, notes, and snippets.

@ronnylt
Created September 26, 2013 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronnylt/6711674 to your computer and use it in GitHub Desktop.
Save ronnylt/6711674 to your computer and use it in GitHub Desktop.
<?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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment