Skip to content

Instantly share code, notes, and snippets.

@samarpanda
Last active December 1, 2023 08:20
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save samarpanda/4125105 to your computer and use it in GitHub Desktop.
Save samarpanda/4125105 to your computer and use it in GitHub Desktop.
Test your php code execution time and Memory usage
<?php
// from http://php.net/manual/en/function.filesize.php
function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
$start = microtime(true);
phpinfo();
return printf("Total time: %s\r\nMemory Used (current): %s\r\nMemory Used (max): %s", round(microtime(true) - $start, 4), formatBytes(memory_get_usage()), formatBytes(memory_get_peak_usage()));
?>
@Aslamkv
Copy link

Aslamkv commented Jun 13, 2021

How about using log function?

$unit=intval(log($bytes,1024));
$units=['B','KB','MB','GB','TB'];
$formatted_bytes=$bytes/pow(1024,$unit);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment