栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

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

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

需求

用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以后方便调用呀。

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);
            }
        }

    }

主要就是调用microtime和memory_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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/228681.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号