|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-08-24 12:42 UTC] quantuumsnot at gmail dot com
Description: ------------ I usually use this short script to test some home-brew functions but noticed a inconsistency in memory_get_usage() and memory_get_peak_usage() Corresponding to docs, when using: FALSE parameter - emalloc() returns bytes used by the script TRUE parameter - returns bytes allocated by OS The problem is that the script worked on PHP 5.6.8 x64 Thread Safe - returned bytes are above 0, but in 7.0.0RC1 this returns 0 The fix is to use at line #1 memory_get_usage(false) which returns 40-50 times!!! bigger memory usage (for example 1.6-1.8MB instead of 40-45KB) Tested it on 3v4l.org and the results are the same, 5.5.0-5.6.12 always win What is the correct way to benchmark my code in PHP7 using pure PHP? (do not suggest xdebug or frameworks, please) Test script: --------------- $memoryUsage = memory_get_peak_usage(false); someFunctionCalled(); $memoryUsage = abs(memory_get_peak_usage(false) - $memoryUsage); PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 01:00:01 2025 UTC |
This problem is valid for devster's 'Ubench' script too: --- <?php error_reporting(-1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); mb_internal_encoding('UTF-8'); mb_http_output('UTF-8'); gc_enable(); // https://github.com/devster/ubench require_once 'src/Ubench.php'; $bench = new Ubench; $testSum = 0; $bench->start(); // Execute some code for ($i = 0; $i < 1000000; $i++) { $j= $i; } $bench->end(); // Get elapsed time and memory echo "<br />" . $bench->getTime(); echo "<br />" . $bench->getTime(true); // elapsed microtime in float echo "<br />" . $bench->getTime(false, '%d%s'); echo "<br />" . $bench->getMemoryPeak(); echo "<br />" . $bench->getMemoryPeak(true); // memory peak in bytes echo "<br />" . $bench->getMemoryPeak(false, '%.3f%s'); // Returns the memory usage at the end mark echo "<br />" . $bench->getMemoryUsage(); exit(); ?>