memory_get_usage()& microtime中()

时间:2016-05-17 15:01:09

标签: php memory microtime

美好的一天!我有这段代码:

final class bench
{
    protected static $start_time;

    protected static $start_memory;

   public static function start()
   {
        self::$start_time   = microtime(true);
        self::$start_memory = memory_get_usage(true)/1024;
   }

   public static function finish()
   {
       echo PHP_EOL.'+'.round(memory_get_usage(true)/1024 - self::$start_memory, 3).' Kb load';
       echo PHP_EOL.'+'.round(microtime(true) - self::$start_time, 3).' Time.';
   }
}

但是当我想在这里使用这个小代码时:

bench::start();

$array = ['f', 'g', 'f', 'd', 'ff'];

foreach($array as $key=>$value)
{
    echo $key.'f'.$value;
}

bench::finish();

我的结果很糟糕。它说我+0 Kb负载+0时间。

所以我尝试使用这个例子:

$start = memory_get_usage()/1024 . "\n";

for ($i = 1; $i <= 100; $i++) {
    echo $i;
}

echo '<br/>+'.round(memory_get_usage()/1024 - $start, 3).' Kb load';

然后我得到了正常的结果。为什么这样?可能有一些比上面代码更好的东西

1 个答案:

答案 0 :(得分:0)

您的代码正常运行。使用的内存是几个字节;由于将除法四舍五入为3位数,因此显示0kb。

相关问题