Created
August 31, 2018 03:33
-
-
Save froger-me/51db0515c98e0c697efb79d3bf5d0c41 to your computer and use it in GitHub Desktop.
Test memory usage in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_formatted_memory_peak( $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 /= ( 1 << ( 10 * $pow ) ); | |
return round( $bytes, $precision ) . ' ' . $units[ $pow ]; | |
} | |
$mem_before = memory_get_peak_usage(); | |
// Some code here to test | |
$mem_after = memory_get_peak_usage(); | |
error_log( get_formatted_memory_peak( $mem_after - $mem_before ) ); //@codingStandardsIgnoreLine |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment