Maison >développement back-end >tutoriel php >PHP 性能的微观分析_PHP教程
$start= microtime(true); echo $start."\n"; $end = microtime(true); echo $end."\n"; echo ($end-$start)."\n";
$base_memory= memory_get_usage(); echo "Hello,world!\n"; $end_memory= memory_get_usage(); $peak_memory= memory_get_peak_usage(); echo $base_memory,"\t",$end_memory,"\t",($end_memory-$base_memory),"\t", $peak_memory,"\n";
$baseMemory= memory_get_usage(); class User { private $uid; function __construct($uid) { $this->uid= $uid; } } for($i=0;$i<100000;$i++) { $obj= new User($i); if ( $i% 10000 === 0 ) { echo sprintf( '%6d: ', $i), memory_get_usage(), " bytes\n"; } } echo " peak: ",memory_get_peak_usage(true), " bytes\n";
[root@localhostphpperf]# php52 memory.php 0: 93784 bytes 10000: 93784 bytes …… 80000: 93784 bytes 90000: 93784 bytes peak: 262144 bytes
[root@localhostphpperf]# phpmemory.php 0: 634992 bytes 10000: 634992 bytes …… 80000: 634992 bytes 90000: 634992 bytes peak: 786432 bytes
[root@localhostphpperf]# php56 memory.php 0: 224944 bytes 10000: 224920 bytes …… 80000: 224920 bytes 90000: 224920 bytes peak: 262144 bytes
[root@localhostphpperf]# php7 memory.php 0: 353912 bytes 10000: 353912 bytes …… 80000: 353912 bytes 90000: 353912 bytes peak: 2097152 bytes
$baseMemory= memory_get_usage(); class User { private $uid; function __construct($uid) { $this->uid= $uid; } } for($i=0;$i<100000;$i++) { $obj= new User($i); $obj->self = $obj; if ( $i% 5000 === 0 ) { echo sprintf( '%6d: ', $i), memory_get_usage(), " bytes\n"; } }