Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中,如何检查某个循环使用了多少内存_Php_Benchmarking - Fatal编程技术网

在PHP中,如何检查某个循环使用了多少内存

在PHP中,如何检查某个循环使用了多少内存,php,benchmarking,Php,Benchmarking,我目前正在制作一个PHP计时器,用来检查一种特定的方法与另一种方法相比需要多少时间来完成相同的工作,基本上是基准测试 现在,我还想让这个工具能够告诉你具体的方法占用了多少内存 所以,更多信息,我用微时间来检查开始时间,在代码上做20万次循环,然后再做一次微时间,用一些数学来检查它花费了多少时间,所以我想做的是,在微时间范围之外,也检查内存使用情况 这是我当前的代码: // Set the amount of loops to do, default is 2000 $loops = 2000;

我目前正在制作一个PHP计时器,用来检查一种特定的方法与另一种方法相比需要多少时间来完成相同的工作,基本上是基准测试

现在,我还想让这个工具能够告诉你具体的方法占用了多少内存

所以,更多信息,我用微时间来检查开始时间,在代码上做20万次循环,然后再做一次微时间,用一些数学来检查它花费了多少时间,所以我想做的是,在微时间范围之外,也检查内存使用情况

这是我当前的代码:
// Set the amount of loops to do, default is 2000
$loops = 2000;
$start_time = microtime(true); // Start the timer

for($i = 0; $i < $loops; $i++) {
    // Code to test
}

$total_time = microtime(true) - $start_time; // Stop the timer, and figure out the total

ob_end_flush();   // Enable output again
echo $total_time; // Echo the timer's result
?>
//设置要执行的循环数量,默认值为2000
$loops=2000;
$start_time=microtime(真);//启动计时器
对于($i=0;$i<$loops;$i++){
//要测试的代码
}
$total_time=微时间(true)-$start_time;//停止计时器,算出总数
ob_end_flush();//再次启用输出
echo$total_time;//回显计时器的结果
?>

您可以使用
内存\u获取\u使用情况
(http://php.net/manual/en/function.memory-get-usage.php)当你认为这一过程达到顶峰时

或者你也可以偶尔调用它并记录最高值。。。或者随你的便

但这是一个过程。你是说一个PHP进程“a”检查另一个PHP进程的内存使用情况吗

如果是:

$myPID = getmypid();
$stats = explode("\n", shell_exec('pmap $(pgrep php) | grep \'total\\|\\:\''));
for ($i = 0; $i < count($stats); $i += 2) {
    if (strpos($stats[$i], "$myPID") === false) {
        preg_match('/\d+/', $stats[$i+1], $preRes);
        $res = $preRes[0];
    }
}
//if $res has a value, that value is the kilobytes of memory being used by the other PHP process
$myPID=getmypid();
$stats=explode(“\n”,shell_exec('pmap$(pgrep php)| grep'total\\\\\\:\”);
对于($i=0;$i
这个解决方案有一个问题:如果总共有两个以上的php进程在运行,就不能保证得到正确的进程

要解决这个问题,首先运行另一个进程,获取它的PID,然后将它作为参数传递给这个进程。如果您有要检查的进程PID,可以执行以下操作:

$stats = explode("\n", shell_exec('pmap $(pgrep php) | grep \'total\\|\\:\''));
for ($i = 0; $i < count($stats); $i += 2) {
    if (strpos($stats[$i], "$otherPID") === 0) {
        preg_match('/\d+/', $stats[$i+1], $preRes);
        $res = $preRes[0];
    }
}
//$res contains the result you want in kilobytes
$stats=explode(“\n”,shell_exec('pmap$(pgrep php)\'grep'total\\\\\\\:\”);
对于($i=0;$i
您也可以检查内存中所有不是您的进程:

$myPID = getmypid();
$stats = explode("\n", shell_exec('pmap $(pgrep php) | grep \'total\\|\\:\''));
for ($i = 0; $i < count($stats) - 1; $i += 2) {
    if (strpos($stats[$i], "$myPID") === false) {
        preg_match('/\d+/', $stats[$i+1], $preRes);
        $res[] = $preRes[0];
    }
}
$myPID=getmypid();
$stats=explode(“\n”,shell_exec('pmap$(pgrep php)| grep'total\\\\\\:\”);
对于($i=0;$i<计数($stats)-1;$i+=2){
if(strpos($stats[$i],“$myPID”)==false){
预匹配('/\d+/',$stats[$i+1],$preRes);
$res[]=$preRes[0];
}
}

因此,要获得最大内存使用量,只需保留一个$max变量并不断检查它。

如果您至少使用5.2,
memory\u get\u peak\u usage()
应该可以正常工作

您可以在循环之前调用它一次,以了解到该点之前的基线,然后在循环执行期间再次调用它以查看峰值

正在修改您的代码

// Set the amount of loops to do, default is 2000
$loops = 2000;
$base_mem = memory_get_peak_usage();
$start_time = microtime(true); // Start the timer

for($i = 0; $i < $loops; $i++) {
    // Code to test
}
$end_time = microtime(true);  // Stop the timer
$extra_mem = memory_get_peak_usage();

// figure out the totals
$total_time = $end_time - $start_time;
$total_mem = $extra_mem - $base_mem;

ob_end_flush();   // Enable output again
echo "Total Time: $total_time\n";
echo "Total Mem Above Basline: $total_mem bytes\n";
//设置要执行的循环数量,默认值为2000
$loops=2000;
$base_mem=内存获取峰值使用率();
$start_time=microtime(真);//启动计时器
对于($i=0;$i<$loops;$i++){
//要测试的代码
}
$end_time=微时间(真);//停止计时
$extra_mem=内存获取峰值使用率();
//算出总数
$total_time=$end_time-$start_time;
$total_mem=$extra_mem-$base_mem;
ob_end_flush();//再次启用输出
echo“总时间:$Total\u Time\n”;
echo“基线上的总内存:$Total_Mem bytes\n”;

我用我的代码更新了这个问题,是的,我说的是进程A检查进程B的内存使用情况。是否必须使用PHP完成?如果是,它会在Linux操作系统上运行还是在Windows下运行?是的,它必须是PHP,它不会在Windows上运行,它会在UNIX上运行,它不会在Mac OS X服务器上运行。这就是我所知道的。酷,期待着一个工作脚本。如果解决方案不容易,我可能只需要每隔200个循环检查一次内存使用情况,然后做一些数学计算。非常感谢您的帮助。:)我也得找点东西!我通常跳过这个问题,只是等别人回答。我希望这能满足你的需要。。。很抱歉没有提供完美的解决方案。谢谢您的回答!是的,如果解决方案不简单,我可能会这样做。:)