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 YoSZ7
Output for 7.3.0alpha1
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000666</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.011134</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000758</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/brHsW on line 48 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006931</b> Memory peak: 3.077 0.020082</b>
Output for 7.2.6
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000588</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.011286</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000640</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/brHsW on line 48 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006501</b> Memory peak: 3.074 0.019398</b>
Output for 7.2.5
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.011033</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000700</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/brHsW on line 48 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006711</b> Memory peak: 3.074 0.019688</b>
Output for 7.2.4
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000778</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.002563</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000709</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/brHsW on line 48 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007090</b> Memory peak: 3.074 0.012004</b>
Output for 7.2.3
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000838</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.002523</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000966</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/brHsW on line 48 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007211</b> Memory peak: 3.074 0.012203</b>
Output for 7.2.2
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.001578</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.112733</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000825</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/brHsW on line 48 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.009370</b> Memory peak: 3.074 0.125118</b>
Output for 7.2.1
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000819</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.015525</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000990</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/brHsW on line 48 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.008309</b> Memory peak: 3.074 0.026130</b>
Output for 7.2.0
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.002999</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.016899</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000727</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/brHsW on line 48 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007342</b> Memory peak: 3.074 0.028558</b>
Output for 7.1.17
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.020919</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.001032</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.011563</b> Memory peak: 3.039 0.034829</b>
Output for 7.1.16
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000741</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.032966</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 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.016838</b> Memory peak: 3.039 0.052607</b>
Output for 7.1.15
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000558</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.015798</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.001052</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.009660</b> Memory peak: 3.039 0.027572</b>
Output for 7.1.14
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.019201</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.001795</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.010946</b> Memory peak: 3.039 0.033383</b>
Output for 7.1.13
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000666</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.012695</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000826</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007350</b> Memory peak: 3.039 0.022165</b>
Output for 7.1.12
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000644</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.011682</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000692</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007077</b> Memory peak: 3.039 0.020734</b>
Output for 7.1.11
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000677</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.011380</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000836</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007468</b> Memory peak: 3.039 0.021159</b>
Output for 7.1.10
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.011113</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 while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007354</b> Memory peak: 3.039 0.020029</b>
Output for 7.1.9
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000681</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.015903</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000733</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007206</b> Memory peak: 3.039 0.025141</b>
Output for 7.1.8
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000712</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.012079</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000624</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006455</b> Memory peak: 3.039 0.020242</b>
Output for 7.1.7
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000575</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.013938</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000980</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.011043</b> Memory peak: 3.039 0.026899</b>
Output for 7.1.6
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.014329</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000875</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.008022</b> Memory peak: 3.041 0.024636</b>
Output for 7.1.5
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000709</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.012737</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000756</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006580</b> Memory peak: 3.041 0.021496</b>
Output for 7.1.4
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000658</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.012461</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000828</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006370</b> Memory peak: 3.041 0.020989</b>
Output for 7.1.3
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000659</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.013265</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000740</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006688</b> Memory peak: 3.041 0.021706</b>
Output for 7.1.2
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000625</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.013010</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000646</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.006471</b> Memory peak: 3.041 0.021125</b>
Output for 7.1.1
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000597</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.011579</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.007043</b> Memory peak: 3.039 0.020239</b>
Output for 7.1.0
foreach before loop mem: 2.00 Mb foreach after loop mem: 2.00 Mb foreach: <b>0.000615</b> foreach ref before loop mem: 2.00 Mb foreach ref after loop mem: 4.00 Mb foreach ref: <b>0.011210</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000780</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.007380</b> Memory peak: 3.039 0.020545</b>
Output for 7.0.30
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.012042</b> for before loop mem: 4.00 Mb for after loop mem: 4.00 Mb for: <b>0.000825</b> while each -> list before loop mem: 4.00 Mb while each -> list after loop mem: 4.00 Mb while each -> list: <b>0.008455</b> Memory peak: 3.04 0.022624</b>
Output for hhvm-3.22.0
foreach before loop mem: 834.89 Kb foreach after loop mem: 835.02 Kb foreach: <b>0.001611</b> foreach ref before loop mem: 836.09 Kb foreach ref after loop mem: 2.23 Mb foreach ref: <b>0.002530</b> for before loop mem: 837.17 Kb for after loop mem: 837.17 Kb for: <b>0.003318</b> while each -> list before loop mem: 839.11 Kb while each -> list after loop mem: 1.32 Mb while each -> list: <b>0.011548</b> Memory peak: 4 0.020560</b>
Output for hhvm-3.21.3
foreach before loop mem: 844.21 Kb foreach after loop mem: 844.34 Kb foreach: <b>0.001436</b> foreach ref before loop mem: 845.41 Kb foreach ref after loop mem: 2.01 Mb foreach ref: <b>0.002791</b> for before loop mem: 846.49 Kb for after loop mem: 846.49 Kb for: <b>0.003499</b> while each -> list before loop mem: 848.43 Kb while each -> list after loop mem: 1.33 Mb while each -> list: <b>0.014132</b> Memory peak: 4 0.023207</b>
Output for hhvm-3.18.5
foreach before loop mem: 844.30 Kb foreach after loop mem: 844.42 Kb foreach: <b>0.001461</b> foreach ref before loop mem: 845.47 Kb foreach ref after loop mem: 2.01 Mb foreach ref: <b>0.002704</b> for before loop mem: 846.52 Kb for after loop mem: 846.52 Kb for: <b>0.003344</b> while each -> list before loop mem: 848.52 Kb while each -> list after loop mem: 1.33 Mb while each -> list: <b>0.010116</b> Memory peak: 4 0.019052</b>
Output for 5.6.30
foreach before loop mem: 4.75 Mb foreach after loop mem: 5.00 Mb foreach: <b>0.006773</b> foreach ref before loop mem: 5.00 Mb foreach ref after loop mem: 9.25 Mb foreach ref: <b>0.010060</b> for before loop mem: 7.50 Mb for after loop mem: 7.50 Mb for: <b>0.002139</b> while each -> list before loop mem: 7.50 Mb while each -> list after loop mem: 8.00 Mb while each -> list: <b>0.016939</b> Memory peak: 8.511 0.041845</b>