2016-05-17 29 views
0

美好的一天!我有這樣的代碼:memory_get_usage()&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'; 

然後我得到正常的結果。爲什麼這樣?可能有,比上面的代碼更好

回答

0

您的代碼正在工作。使用的內存是幾個字節;由於將分數舍入爲3位數,因此顯示0kb。

+0

對不起,但我不明白你。什麼是「顯示0 kb」的含義?我試圖改變3到5,但它並沒有幫助我 – LittleByte

+0

內存使用率是如此之低,當你除以1024,然後圍繞它,你可以看到的是0。嘗試顯示內存使用差異沒有劃分或舍入任何東西。 – JesusTheHun

+0

非常感謝! – LittleByte