Created
May 29, 2014 10:32
-
-
Save anonymous/aaf845ae354578b74906 to your computer and use it in GitHub Desktop.
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 function_pass_by_value($a) { | |
// Will trigger copy-on-write! WOW! | |
$a[] = 101; | |
} | |
function function_pass_by_reference($a) { | |
// Readonly. Will not trigger copy-on-write! | |
// $a is passed by reference. | |
$b = $a[101]; | |
} | |
$array = array_fill(0, 100000, 123); | |
$x = microtime(true); | |
for ($i = 0; $i < 1000; $i++) { | |
function_pass_by_reference($array); | |
} | |
$diff = microtime(true) - $x; | |
echo count($array) . " : pass by reference : $diff\n"; | |
$x = microtime(true); | |
for ($i = 0; $i < 1000; $i++) { | |
function_pass_by_value($array); | |
} | |
$diff = microtime(true) - $x; | |
echo count($array) . " : pass by value : $diff\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment