3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $start = microtime(true); function bytesToSize($bytes, $precision = 2) { $symbols = array('B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb'); $exp = (int)floor(log($bytes) / log(1024)); return sprintf('%.'. $precision .'f '. $symbols[$exp], $bytes / pow(1024, floor($exp))); } function mem() { return bytesToSize(memory_get_usage(true)); } function test($msg, $test) { $t = microtime(true); $test($msg); echo $msg .': <b>'. sprintf('%.6f', microtime(true) - $t) .'</b>'. PHP_EOL; } $array = range(0, 30000); test('foreach', function ($msg) use ($array) { echo $msg . ' before loop mem: '. mem() .PHP_EOL; foreach ($array as $v) { $b = $v; } echo $msg . ' after loop mem: '. mem() .PHP_EOL; }); test('foreach ref', function ($msg) use ($array) { echo $msg . ' before loop mem: '. mem() .PHP_EOL; foreach ($array as &$v) { $b = $v; } unset($v); echo $msg . ' after loop mem: '. mem() .PHP_EOL; }); test('for', function ($msg) use ($array) { echo $msg . ' before loop mem: '. mem() .PHP_EOL; $so = count($array); for ($i = 0; $i < $so; ++$i) { $b = $array[$i]; } echo $msg . ' after loop mem: '. mem() .PHP_EOL; }); test('while each -> list', function ($msg) use ($array) { echo $msg . ' before loop mem: '. mem() .PHP_EOL; while (list($k, $v) = each($array)) { $b =$v; } echo $msg . ' after loop mem: '. mem() .PHP_EOL; }); echo 'Memory peak: ' . round(memory_get_peak_usage() / 1048576, 3) .PHP_EOL; echo sprintf('%.6f', microtime(true) - $start) .'</b>'. PHP_EOL;
based on brHsW
Output for 7.3.0alpha1
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000570</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.013563</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000590</b> while each -> list before loop mem: 4.00 Mb Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/qnRTs on line 48 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006946</b> Memory peak: 3.077 0.022076</b>
Output for 7.2.6
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000703</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.013539</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000630</b> while each -> list before loop mem: 4.00 Mb Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/qnRTs on line 48 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006716</b> Memory peak: 3.074 0.021950</b>
Output for 7.2.5
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000592</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.011318</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000605</b> while each -> list before loop mem: 4.00 Mb Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/qnRTs on line 48 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006971</b> Memory peak: 3.074 0.019860</b>
Output for 7.2.4
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000753</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.028230</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000750</b> while each -> list before loop mem: 4.00 Mb Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/qnRTs on line 48 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007845</b> Memory peak: 3.074 0.038205</b>
Output for 7.2.3
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000661</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.011819</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000841</b> while each -> list before loop mem: 4.00 Mb Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/qnRTs on line 48 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007201</b> Memory peak: 3.074 0.020883</b>
Output for 7.2.2
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000739</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.013680</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.001023</b> while each -> list before loop mem: 4.00 Mb Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/qnRTs on line 48 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.008770</b> Memory peak: 3.074 0.024947</b>
Output for 7.2.1
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000870</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.012983</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000653</b> while each -> list before loop mem: 4.00 Mb Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/qnRTs on line 48 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007243</b> Memory peak: 3.074 0.022183</b>
Output for 7.2.0
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000710</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.016742</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.001231</b> while each -> list before loop mem: 4.00 Mb Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/qnRTs on line 48 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.010526</b> Memory peak: 3.074 0.030497</b>
Output for 7.1.17
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000633</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.010778</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000741</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007393</b> Memory peak: 3.039 0.019956</b>
Output for 7.1.16
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000742</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.013766</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000745</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007248</b> Memory peak: 3.039 0.023177</b>
Output for 7.1.15
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000938</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.012325</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000789</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006969</b> Memory peak: 3.039 0.021652</b>
Output for 7.1.14
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000622</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.012941</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000693</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007689</b> Memory peak: 3.039 0.022274</b>
Output for 7.1.13
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000989</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.141736</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000695</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006609</b> Memory peak: 3.039 0.150491</b>
Output for 7.1.12
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000673</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.011342</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000672</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.008495</b> Memory peak: 3.039 0.021588</b>
Output for 7.1.11
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000593</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.010981</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000817</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.008297</b> Memory peak: 3.039 0.021139</b>
Output for 7.1.10
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000791</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.014213</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.001403</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.009186</b> Memory peak: 3.039 0.026421</b>
Output for 7.1.9
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000894</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.026270</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.001308</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.011249</b> Memory peak: 3.039 0.040122</b>
Output for 7.1.8
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000593</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.012715</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000708</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006818</b> Memory peak: 3.039 0.021239</b>
Output for 7.1.7
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000641</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.017718</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.001109</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.010070</b> Memory peak: 3.039 0.030232</b>
Output for 7.1.6
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000562</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.013272</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000742</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006409</b> Memory peak: 3.041 0.021346</b>
Output for 7.1.5
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000662</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.012901</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000689</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006490</b> Memory peak: 3.041 0.021396</b>
Output for 7.1.4
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000739</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.015195</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000776</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007521</b> Memory peak: 3.041 0.024862</b>
Output for 7.1.3
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000896</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.012699</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000694</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006557</b> Memory peak: 3.041 0.021446</b>
Output for 7.1.2
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000645</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.012149</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000783</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006584</b> Memory peak: 3.041 0.020821</b>
Output for 7.1.1
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000579</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.011606</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000608</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006305</b> Memory peak: 3.039 0.019441</b>
Output for 7.1.0
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000704</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.017169</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000992</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.010966</b> Memory peak: 3.039 0.030194</b>
Output for 7.0.30
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000642</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.010892</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000800</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007913</b> Memory peak: 3.04 0.020895</b>
Output for hhvm-3.22.0
foreach before loop mem: 834.89 Kb foreach after loop mem: 835.02 Kb foreach: <b>0.001424</b> foreach ref before loop mem: 836.09 Kb foreach ref after loop mem: 2.23 Mb foreach ref: <b>0.002633</b> for before loop mem: 837.17 Kb for after loop mem: 837.17 Kb for: <b>0.003355</b> while each -> list before loop mem: 839.11 Kb while each -> list after loop mem: 1.32 Mb while each -> list: <b>0.012096</b> Memory peak: 4 0.020862</b>
Output for hhvm-3.21.3
foreach before loop mem: 844.21 Kb foreach after loop mem: 844.34 Kb foreach: <b>0.001453</b> foreach ref before loop mem: 845.41 Kb foreach ref after loop mem: 2.01 Mb foreach ref: <b>0.002653</b> for before loop mem: 846.49 Kb for after loop mem: 846.49 Kb for: <b>0.003206</b> while each -> list before loop mem: 848.43 Kb while each -> list after loop mem: 1.33 Mb while each -> list: <b>0.013166</b> Memory peak: 4 0.021832</b>
Output for hhvm-3.18.5
foreach before loop mem: 844.30 Kb foreach after loop mem: 844.42 Kb foreach: <b>0.001490</b> foreach ref before loop mem: 845.47 Kb foreach ref after loop mem: 2.01 Mb foreach ref: <b>0.002648</b> for before loop mem: 846.52 Kb for after loop mem: 846.52 Kb for: <b>0.003290</b> while each -> list before loop mem: 848.52 Kb while each -> list after loop mem: 1.33 Mb while each -> list: <b>0.010386</b> Memory peak: 4 0.019130</b>
Output for 5.6.30
foreach before loop mem: 4.75 Mb foreach after loop mem: 5.00 Mb foreach: <b>0.008733</b> foreach ref before loop mem: 5.00 Mb foreach ref after loop mem: 9.25 Mb foreach ref: <b>0.011660</b> for before loop mem: 7.50 Mb for after loop mem: 7.50 Mb for: <b>0.002689</b> while each -> list before loop mem: 7.50 Mb while each -> list after loop mem: 8.00 Mb while each -> list: <b>0.025743</b> Memory peak: 8.511 0.057072</b>