3

是否有可能在 PHP 中获得特定代码块的峰值内存使用量?该memory_get_peak_usage()函数似乎在整个过程执行中达到了峰值,直到函数调用为止,但这不是我想要获得的,因为其他代码块可能会扭曲该值。我试图隔离代码块本身,而不是整个过程。

例子:

// Block 1
for ($i = 0; $i < $iterations; ++$i) {
    // some code
}

// Block 2
for ($i = 0; $i < $iterations; ++$i) {
    // some different code
}

// Determine which of the two blocks used the most memory during their execution here

不幸的是,xdebug 目前不是我的选择。

4

2 回答 2

-1

I don't know a specific function that does this, but if you are just trying to isolate a block of code does something as simple as:

$before = memory_get_peak_usage();
for ($i = 0; $i < $iterations; ++$i) {
    // some code
}
$after = memory_get_peak_usage();
$used_memory = $after - $before;
于 2013-02-19T22:07:46.560 回答
-1

编辑:XHProf 做到了,在这里查看我的答案


不要使用memory_get_peak_usage()

$alpha = memory_get_usage();

for ($i = 0; $i < $iterations; ++$i) {
    // some code
}

$used_memory = memory_get_usage() - $alpha;

请记住,这只会返回您需要的最终内存量。// some code中间内存消耗(例如设置/销毁东西或调用函数)不会计算在内。

您可以使用自己的方式破解,register_tick_function()但它仍然不适用于函数调用。

于 2013-05-12T20:18:15.197 回答