<?php
echo "Initial: ".memory_get_usage()." bytes n";
/* prints
Initial: 361400 bytes
*/
// let's use up some memory
for ($i = 0; $i < 100000; $i++) {
$array []= md5($i);
}
// let's remove half of the array
for ($i = 0; $i < 100000; $i++) {
unset($array[$i]);
}
echo "Final: ".memory_get_usage()." bytes n";
/* prints
Final: 885912 bytes
*/
echo "Peak: ".memory_get_peak_usage()." bytes n";
/* prints
Peak: 13687072 bytes
*/
?>
این هم یک کد مشابه که زمان اجرای اسکریپت رو هم اضافه کرده و میتونید خیلی ساده و حدودی میزان مصرف منابع اسکریپتتون رو چک کنید:
<?php
$time_start = microtime(true);
function convert($size)
{
$unit=array('B','KB','MB','GB','TB','PB');
return round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
$initialusage=convert(memory_get_usage());
// your code here
$peakusage=convert(memory_get_peak_usage());
$finalusage=convert(memory_get_usage());
$time_end = microtime(true);
$time = $time_end - $time_start;
$usagecolorfunctionargu = "";
function usagecolor($usagecolorfunctionargu){
if (strpos($usagecolorfunctionargu,'KB') !== false) {
return 'green';
}
elseif (strpos($usagecolorfunctionargu,'MB') !== false) {
return 'red';
}
}
echo '<div style="float:right;"><table style="border:1px solid black;">';
echo '<tr><td><strong>Initial Memory:</strong></td><td style="color:'. usagecolor($initialusage) .';">' . $initialusage . '</td></tr>';
echo '<tr><td><strong>Peak Memory:</strong></td><td style="color:'. usagecolor($peakusage) .';">' . $peakusage . '</td></tr>';
echo '<tr><td><strong>Final Memory:</strong></td><td style="color:'. usagecolor($finalusage) .';">' . $finalusage . '</td></tr>';
echo '<tr><td><strong>Time (s):</strong></td><td style="color:';
if($time <1){
echo 'green';
}
else{
echo 'red';
}
echo ';">' . round($time, 4) .' seconds' . '</td></tr>';
echo '</table></div>';
?>