Skip to content

Instantly share code, notes, and snippets.

@froger-me
Created August 31, 2018 03:33
Show Gist options
  • Save froger-me/51db0515c98e0c697efb79d3bf5d0c41 to your computer and use it in GitHub Desktop.
Save froger-me/51db0515c98e0c697efb79d3bf5d0c41 to your computer and use it in GitHub Desktop.
Test memory usage in PHP
<?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