感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

PHP实现程序计时和内存统计类

BIG阳
关注TA
已关注
手记 473
粉丝 73
获赞 458

需求

用PHP搭建web程序的时候很常见的一点就是要统计程序运行的时间来评估一段PHP是否好坏,而PHP也提供microtime()这样的函数来轻松的获取运行时间,而我们最好的方式就是“别人写好,拿来主义”,这也避免了重复造轮子的尴尬,极大地提升了程序开发的效率。

具体实践

首先我们看下java的获取运行时间的代码

//java
  public final void getTime() {        long start = System.currentTimeMillis();
        runcode();        long end = System.currentTimeMillis();
        System.out.println("运行时间:" + (end - start) + "毫秒");//应该是end - start
    }

可以发现是基本思路是设置两个时间锚点,最后两个时间相减就好,我们也封装一个这样的PHP 的class以后方便调用呀。

<?php/**
* @author:michaelgbw
* @version:1.0 (2016-06-17)
*
*/header('content-type:text/html;charset=utf-8');    class codeStatus{        /**
        * @param:boolen $time_type时间类型, boolen$show_m是否显示内存,
        * boolen$show_type,false是array,ture为json,$regmax是数据保留几位
        *
        *
        */
        public $regmax;        public $runTime=0;        public $time_type;        public $is_show_memory;        public $show_type;        private static $t1;        private static $t2;        public function __construct($time_type = false , $show_m= true, $show_type = false,$regmax=3){            $this->regmax=$regmax;            $this->is_show_memory=$show_m;            $this->time_type=$time_type;            $this->show_type=$show_type;
        }        public function timeStart(){            self::$t1=microtime(true);
        }        public function timeEnd(){            self::$t2=microtime(true);
        }        private function getTime(){            if(self::$t1 != '' && self::$t2 !=''){                if($this->time_type== false ){                    return  '耗时'.round(self::$t2-self::$t1,$this->regmax).'秒';
                }                else if($this->time_type == true){                    return '耗时'.round(self::$t2 - self::$t1,($this->regmax + 3)) * 1000 .'毫秒';
                }
            }            else{                return "error param";
            }
        }        private function getmemory(){
            $memory     = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024, $this->regmax).'KB'; 
                return '占用内存:'  . $memory;
        }        public function showStatus(){            if($this->is_show_memory == true){
                $arr=array(self::getTime(),self::getmemory());
            }            else{
                $arr=array(self::getTime());
            }            if($this->show_type == false){                return $arr;
            }            else{                return json_encode($arr);
            }
        }

    }

主要就是调用microtimememory_get_usagel来进行相关操作。我们用这个来测试下吧:

///////////////////test///////////////
    $c= new codeStatus(true);
    $c->timeStart();
    $arr=array();    for ($i=0; $i <100; $i++) { 
        $arr[]=rand(0,5000);
    }    function selectsort($array){
      $count=count($array);      if($count<=0)        return false;      else{        for($i=0;$i<$count;$i++)            for($j=$i+1;$j<$count;$j++){                if($array[$i]>$array[$j]){
                    $tmp=$array[$j];
                    $array[$j]=$array[$i];
                    $array[$i]=$tmp;
                }
            }
      }      return $array;
    }
    selectsort($arr);
    $c->timeEnd();
    print_r($c->showStatus());

测试一个选择排序的运行时间和内存占用

结果

还行吧,发现好多常用操作,自己封装下还是蛮方便的,今后开发什么的直接就用,哈哈~



作者:michaelgbw
链接:https://www.jianshu.com/p/96c5641a98b3

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP