码迷,mamicode.com
首页 > Web开发 > 详细

php代码性能分析方法

时间:2015-11-13 20:49:39      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:

1.用到的函数

 

microtime()  ,函数返回当前 Unix 时间戳和微秒数,本函数以 "msec sec" 的格式返回一个字符串,其中 sec 是自 Unix 纪元(0:00:00 January 1, 1970 GMT)起到现在的秒数,msec 是微秒部分。字符串的两部分都是以秒为单位返回的

 

memory_get_usage()  ,函数返回内存使用量,还可以有个参数,$real_usage,其值为布尔值。如果设置为 TRUE,获取系统分配的真实内存尺寸。如果未设置或者设置为 FALSE,将是 emalloc() 报告使用的内存量,单位为 byte(s),函数需要在Linux上运行。

 

其他相关函数:

unset   清除变量的内存占用
mysql_free_result($result)    $result$result = mysql_query($sql,$con);
memory_get_peak_usage()        函数返回内存使用峰值,函数需要在Linux上运行
getrusage()   返回CUP使用情况,函数需要在Linux上运行    

 

2.实例代码

//将可以将memory_get_usage()函数返回的byte为单位的内存使用量,转化为M为单位,本例子中没有用

  function memory_usage() {

   $memory = ( ! function_exists(‘memory_get_usage‘)) ? ‘0‘ : round(memory_get_usage()/1024/1024, 2).‘MB‘;

     return $memory;

  }


//得到加上微妙在内的准确的时间戳
function
microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } class t{ static $start_time; static $end_time; static $start_memory; static $end_memory; public static function start() { self::$start_memory = memory_get_usage(); //单位为 byte(s) self::$start_time = microtime_float(); echo ‘<br/>Start @‘.self::$start_time.‘(‘.self::$start_memory.‘)|------->‘; } public static function end() { self::$end_time = microtime_float(); self::$end_memory = memory_get_usage(); echo ‘End @‘.self::$end_time.‘(‘.self::$end_memory.‘) :‘; echo ‘|======= 共耗时:‘.(self::$end_time-self::$start_time).‘,共用内存:‘.(self::$end_memory-self::$start_memory); } }


  //消除t类首次加载的影响
  t::start();
  t::end();


   t::start();
   $str = "我来到你的城市走过你来时的路,想象着没我的日子你是怎样的孤独";
   t::end();

   显示结果:

   Start @1447408386.0921(242528)|------->End @1447408386.0922(242720) :|======= 共耗时:3.6001205444336E-5,共用内存:192
   Start @1447408386.0922(242720)|------->End @1447408386.0922(242856) :|======= 共耗时:5.0067901611328E-6,共用内存:136

 

 

php代码性能分析方法

标签:

原文地址:http://www.cnblogs.com/xiaoyueer/p/4963152.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!