3v4l.org

run code in 300+ PHP versions simultaneously
<?php $PHORUM = array( "OBJ" => new ArrayObject() ); for ($x = 0; $x < 100; $x++) { if ($x % 3 == 0) { $PHORUM[$x] = "XXXXXXXXXXX"; } elseif ($x % 5 == 0) { $PHORUM[$x] = array( "x" => array( "x" => array( "x" => array( "x" => "foo" ) ) ) ); } else { $PHORUM[$x] = rand(1,100); } } function global_copy_from() { global $PHORUM; $foo = $PHORUM[0]; } function GLOBALS_copy_from() { $PHORUM = $GLOBALS["PHORUM"]; $foo = $PHORUM[0]; } $use_copy_from = function() use ($PHORUM) { $foo = $PHORUM[0]; }; function global_check() { global $PHORUM; if ($PHORUM[0] > 1) {} } function GLOBALS_check() { $PHORUM = $GLOBALS["PHORUM"]; if ($PHORUM[0] > 1) {} } $use_check = function() use ($PHORUM) { if ($PHORUM[0] > 1) {} }; function global_change($i) { global $PHORUM; $PHORUM[0] = $i; } function GLOBALS_change($i) { $GLOBALS["PHORUM"][0] = $i; } $use_change = function($i) use (&$PHORUM) { $PHORUM[0] = $i; }; foreach (array("copy_from", "check", "change") as $z) { echo "$z:\n"; foreach (array("global", "GLOBALS", "use") as $x) { $runs = array(); $func = $x."_".$z; if (isset($$func)) { $callable = $$func; if (is_callable($callable)) { $func = $callable; } } for ($y = 0; $y < 31; $y++) { $time_start = microtime(true); for ($i=0; $i < 100; ++$i) { $func($i); } $time_end = microtime(true); $runs[] = round(($time_end - $time_start)*1000000, 3); } sort($runs); $runs = array_slice($runs, floor(count($runs) * 0.05), -1 * (floor(count($runs) * 0.05))); $avg = round((array_sum($runs)/count($runs)), 3); $low = reset($runs); $high = end($runs); $median = $runs[ceil(count($runs)/2)+1]; echo " $x:\n"; echo " avg: ".$avg."µs\n"; echo " median: ".$median."µs\n\n"; } echo "\n"; }

preferences:
26.15 ms | 415 KiB | 5 Q