Last active
August 29, 2015 14:03
-
-
Save bwg/3cfc85018d66759b47d0 to your computer and use it in GitHub Desktop.
testing pass by reference vs. pass by value
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 Version 5.4.29 | |
-------------------------------------------------- | |
Testing large_array over 100,000 iterations: | |
pass_by_ref 95.081090927124 ms | |
pass_by_val 86.126089096069 ms | |
-------------------------------------------------- | |
Testing small_array over 100,000 iterations: | |
pass_by_ref 86.638927459717 ms | |
pass_by_val 85.745096206665 ms | |
-------------------------------------------------- | |
Testing large_object over 100,000 iterations: | |
pass_by_ref 86.174964904785 ms | |
pass_by_val 88.841915130615 ms | |
-------------------------------------------------- | |
Testing large_string over 100,000 iterations: | |
pass_by_ref 80.765962600708 ms | |
pass_by_val 81.165075302124 ms | |
-------------------------------------------------- | |
Testing small_string over 100,000 iterations: | |
pass_by_ref 85.597038269043 ms | |
pass_by_val 83.338022232056 ms | |
-------------------------------------------------- | |
Testing integer over 100,000 iterations: | |
pass_by_ref 84.825038909912 ms | |
pass_by_val 81.841945648193 ms | |
-------------------------------------------------- | |
average (ms) total (ms) | |
-------------------------------------------------- | |
pass_by_ref 86.513837178548 519.08302307129 | |
pass_by_val 84.50969060262 507.05814361572 |
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 | |
$iterations = 100000; | |
$large_array = array_fill(PHP_INT_MAX / 2, 1000, 'a'); | |
$values = [ | |
'large_array' => $large_array, | |
'small_array' => array('this', 'is', 'a', 'small', 'array'), | |
'large_object' => (object)$large_array, | |
'large_string' => str_repeat('a', 100000), | |
'small_string' => 'this is a small string', | |
'integer' => PHP_INT_MAX / 2, | |
]; | |
$totals = [ | |
'pass_by_ref' => 0, | |
'pass_by_val' => 0, | |
]; | |
function pass_by_ref(&$var) {} | |
function pass_by_val($var) {} | |
echo "PHP Version ".phpversion()."\n\n"; | |
foreach ($values as $name => $value) { | |
echo str_repeat('-', 50)."\n"; | |
echo "Testing {$name} over ".number_format($iterations)." iterations:\n\n"; | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
pass_by_ref($value); | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['pass_by_ref'] += $duration; | |
echo str_pad('pass_by_ref', 30)."{$duration} ms\n"; | |
//----------------------------------------------------------------------------- | |
$start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
pass_by_val($value); | |
} | |
$end = microtime(true); | |
$duration = ($end - $start) * 1000; | |
$totals['pass_by_val'] += $duration; | |
echo str_pad('pass_by_val', 30)."{$duration} ms\n"; | |
//----------------------------------------------------------------------------- | |
} | |
echo "\n".str_repeat('-', 50)."\n"; | |
echo str_pad('', 15); | |
echo str_pad('average (ms)', 20); | |
echo str_pad('total (ms)', 20)."\n"; | |
echo str_repeat('-', 50)."\n"; | |
foreach ($totals as $name => $total) { | |
echo str_pad($name, 15); | |
echo str_pad($total/count($values), 20); | |
echo str_pad($total, 20)."\n"; | |
} | |
echo "\n"; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment