Skip to content

Instantly share code, notes, and snippets.

Created May 29, 2014 10:32
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 anonymous/aaf845ae354578b74906 to your computer and use it in GitHub Desktop.
Save anonymous/aaf845ae354578b74906 to your computer and use it in GitHub Desktop.
<?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