Last active
December 19, 2015 20:08
-
-
Save anthonybishopric/6011240 to your computer and use it in GitHub Desktop.
Guess what $counter is!
This file contains 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 | |
function timer(callable $fn, $name, $freq=50000) | |
{ | |
$start = microtime(true); | |
$start_mem = memory_get_usage(true); | |
$things = []; | |
for ($i = 0; $i < $freq; $i++) | |
{ | |
$things[] = $fn(); | |
} | |
$end_mem = memory_get_usage(true); | |
print_r(sprintf("%s took %s seconds and %s bytes\n", $name, microtime(true) - $start, $end_mem - $start_mem)); | |
} | |
$counter = 0; | |
class Destructy | |
{ | |
public function __destruct() | |
{ | |
global $counter; | |
$counter++; | |
} | |
} | |
timer(function() { | |
$a = new Destructy(); | |
$b = new Destructy(); | |
$a->foo = 'hey'; | |
$b->bar = 'there'; | |
unset($a); | |
unset($b); | |
}, 'two variables'); | |
echo "counter is now $counter\n"; | |
timer(function() { | |
$a = new Destructy(); | |
$b = new Destructy(); | |
$a->foo = $b; | |
$b->bar = $a; | |
unset($a); | |
unset($b); | |
}, 'two variables with references to each other'); | |
echo "counter is now $counter\n"; | |
gc_collect_cycles(); | |
echo "counter is now $counter\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment