Created
September 26, 2013 09:04
-
-
Save ronnylt/6711674 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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