zoukankan      html  css  js  c++  java
  • php代码性能分析方法

    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

     

  • 相关阅读:
    xxx.app已损坏,打不开.你应该将它移到废纸篓-已解决
    如何培训新进的软件测试人员
    测试Leader,这些你都做到了嘛?
    GitHub无法访问、443 Operation timed out的解决办法
    GitHub使用Personal access token
    运行Windows虚拟机时,风扇狂转的问题
    使用Keras做OCR时报错:ValueError: Tensor Tensor is not an element of this graph
    Windows虚拟机中无法传输Arduino程序的问题
    Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128)
    实际应用中遇到TimedRotatingFileHandler不滚动的问题
  • 原文地址:https://www.cnblogs.com/xiaoyueer/p/4963152.html
Copyright © 2011-2022 走看看