Created
August 29, 2016 14:13
-
-
Save IvanChepurnyi/9d4d3cfe68eff0f5f38600ce7a26237b 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 | |
ini_set('memory_limit', '2G'); | |
class ExampleByReference | |
{ | |
public function __invoke(&$array) | |
{ | |
foreach ($array as $index => &$value) { | |
$this->itemRoutine($value); | |
} | |
} | |
private function itemRoutine(&$item) | |
{ | |
foreach ($item as $index => &$value) { | |
$this->itemSubRoutine($value); | |
} | |
} | |
private function itemSubRoutine(&$item) | |
{ | |
foreach ($item as $index => &$value) { | |
strlen($value); | |
} | |
} | |
} | |
class ExampleNoReference | |
{ | |
public function __invoke($array) | |
{ | |
foreach ($array as $index => $value) { | |
$this->itemRoutine($value); | |
} | |
} | |
private function itemRoutine($item) | |
{ | |
foreach ($item as $index => $value) { | |
$this->itemSubRoutine($value); | |
} | |
} | |
private function itemSubRoutine($item) | |
{ | |
foreach ($item as $index => $value) { | |
strlen($value); | |
} | |
} | |
} | |
function arrayTree($min, $max, $levels = 2) | |
{ | |
$result = []; | |
if ($levels --) { | |
foreach (range($min, $max) as $index) { | |
$result[$index] = arrayTree($min, rand(20, 30), $levels); | |
} | |
return $result; | |
} | |
$baseString = 'some people think that this string is faster transfered on reference.'; | |
foreach (range($min, $max) as $index) { | |
$result['key_' . $index] = str_shuffle($baseString); | |
} | |
return $result; | |
} | |
$container = new stdClass(); | |
$container->largeArray = arrayTree(1, 10000); | |
$benchmark = function ($name, $callback, $container) { | |
$memory = memory_get_usage(); | |
$time = microtime(true); | |
for ($i = 0; $i < 100; $i ++ ) { | |
$callback($container->largeArray); | |
} | |
$time = microtime(true) - $time; | |
$memory = memory_get_usage() - $memory; | |
echo "======================\n"; | |
echo "Benchark Name: $name\n"; | |
echo "Time: ${time}s\n"; | |
echo "Peak memory: ${memory}\n"; | |
}; | |
$benchmark('Just Variables', new ExampleNoReference(), $container); | |
$benchmark('By Reference', new ExampleByReference(), $container); |
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
Results for PHP7.1 | |
====================== | |
Benchark Name: Just Variables | |
Time: 63.145395994186s | |
Peak memory: 0 | |
====================== | |
Benchark Name: By Reference | |
Time: 74.283300876617s | |
Peak memory: 156487344 | |
Results for PHP7.0 | |
====================== | |
Benchark Name: Just Variables | |
Time: 63.394997119904s | |
Peak memory: 0 | |
====================== | |
Benchark Name: By Reference | |
Time: 73.90741610527s | |
Peak memory: 155995200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment