zoukankan      html  css  js  c++  java
  • php 类静态变量 和 常量消耗内存及时间对比

    在对类执行100w次循环后, 常量最快,变量其次,静态变量消耗时间最高

    其中:

    常量消耗:101.1739毫秒

    变量消耗:2039.7689毫秒

    静态变量消耗:4084.8911毫秒

    测试代码:

    class Timer_profiler {
    
        public static $begin_timer;
        public static $finish_timer;
        public static $timer_html;
    
        /**
         * 计算时间差
         * @return type
         */
        public static function getRecordTimer() {
            return (self::getFinishTimer() - self::getBeginTimer()) * 1000;
        }
    
        /**
         * 生成输出HTML
         * @param type $message
         * @return type
         */
        public static function recordHtml($message = '') {
            self::setFinishTimer();
            return "<p>{$message}耗时: " . self::getRecordTimer() . " 毫秒</p>";
        }
    
        /**
         * 设置开始时间
         */
        public static function setBeginTimer() {
            self::$begin_timer = self::getTime();
        }
    
        /**
         * 设置结束时间
         */
        public static function setFinishTimer() {
            self::$finish_timer = self::getTime();
        }
    
        /**
         * 获取开始时间
         * @return type
         */
        public static function getBeginTimer() {
            return self::$begin_timer;
        }
    
        /**
         * 获取结束时间
         * @return type
         */
        public static function getFinishTimer() {
            return self::$finish_timer;
        }
    
        /**
         * 获取带微妙的时间戳 
         * @return type
         */
        private static function getTime() {
            list($usec, $sec) = explode(" ", microtime());
            return (number_format($sec+$usec,6,'.', ''));
        }
    
    }
    function computeTime($otime,$message){
        return;
        $ntime =  xdebug_time_index();
        $str = '';
        $str .= $message . ($ntime*1000-$otime*1000);
        $str .= "<br>";
        echo $str;
    }
    
    function getMemoryUsed(){
        $str = '消耗内存<h3 style="color:red"> ';
        $str .= round(memory_get_usage()/1024/1024, 4).'MB';
        $str .= '</h3>';
        echo $str;
    }
    
    
    
    $count_i = 100*10000;
    //$count_i = 100000;
    Timer_profiler::setBeginTimer();
    computeTime(xdebug_time_index(),'开始执行');
    echo Timer_profiler::recordHtml('开始执行');
    getMemoryUsed();
    class TestVar {
        public $A1 = 'aaaaaaaaaaaaaaaaa';
        public $A2 = 'aaaaaaaaaaaaaaaaa';
        public $A3 = 'aaaaaaaaaaaaaaaaa';
        public $A4 = 'aaaaaaaaaaaaaaaaa';
        public $A5 = 'aaaaaaaaaaaaaaaaa';
        public $A6 = 'aaaaaaaaaaaaaaaaa';
        public $A7 = 'aaaaaaaaaaaaaaaaa';
        public $A8 = 'aaaaaaaaaaaaaaaaa';
        
    }
    $TestVar = new TestVar();
    for($i=0;$i<=$count_i;$i++){
        $t = $TestVar->A1;
        $t = $TestVar->A2;
        $t = $TestVar->A3;
        $t = $TestVar->A4;
        $t = $TestVar->A5;
        $t = $TestVar->A6;
        $t = $TestVar->A7;
        $t = $TestVar->A8;
    }
    getMemoryUsed();
    echo Timer_profiler::recordHtml('变量完成');
    computeTime(xdebug_time_index(),'变量完成');
    
    
    Timer_profiler::setBeginTimer();
    class TestStaticVar {
        static $A1 = 'aaaaaaaaaaaaaaaaa';
        static $A2 = 'aaaaaaaaaaaaaaaaa';
        static $A3 = 'aaaaaaaaaaaaaaaaa';
        static $A4 = 'aaaaaaaaaaaaaaaaa';
        static $A5 = 'aaaaaaaaaaaaaaaaa';
        static $A6 = 'aaaaaaaaaaaaaaaaa';
        static $A7 = 'aaaaaaaaaaaaaaaaa';
        static $A8 = 'aaaaaaaaaaaaaaaaa';
        
    }
    
    for($i=0;$i<=$count_i;$i++){
        $t = TestStaticVar::$A1;
        $t = TestStaticVar::$A2;
        $t = TestStaticVar::$A3;
        $t = TestStaticVar::$A4;
        $t = TestStaticVar::$A5;
        $t = TestStaticVar::$A6;
        $t = TestStaticVar::$A7;
        $t = TestStaticVar::$A8;
    }
    getMemoryUsed();
    echo Timer_profiler::recordHtml('静态变量完成');
    computeTime(xdebug_time_index(),'静态变量完成');
    
    
    
    
    Timer_profiler::setBeginTimer();
    class TestConstVar {
        const A1 = 'aaaaaaaaaaaaaaaaa';
        const A2 = 'aaaaaaaaaaaaaaaaa';
        const A3 = 'aaaaaaaaaaaaaaaaa';
        const A4 = 'aaaaaaaaaaaaaaaaa';
        const A5 = 'aaaaaaaaaaaaaaaaa';
        const A6 = 'aaaaaaaaaaaaaaaaa';
        const A7 = 'aaaaaaaaaaaaaaaaa';
        const A8 = 'aaaaaaaaaaaaaaaaa';
        
    }
    for($i=0;$i<=$count_i;$i++){
        $t = TestConstVar::A1;
        $t = TestConstVar::A2;
        $t = TestConstVar::A3;
        $t = TestConstVar::A4;
        $t = TestConstVar::A5;
        $t = TestConstVar::A6;
        $t = TestConstVar::A7;
        $t = TestConstVar::A8;
    }
    getMemoryUsed();
    echo Timer_profiler::recordHtml('常量完成');
    computeTime(xdebug_time_index(),'常量完成');
    
    
    //echo Timer_profiler::recordHtml('共执行');
    computeTime(xdebug_time_index(),'共执行');
    exit;
    View Code
  • 相关阅读:
    CentOS7 安装Kafka
    使用ZeroTier搭建大局域网利用VNC远程桌面
    IntelliJ常用配置备忘
    docker-compose部署kafka
    使用Mybatis执行sql脚本
    jQuery实现三级联动菜单(鼠标悬停联动)
    phonegap 使用极光推送实现消息推送
    如何用HTML5+PhoneGap写个Path项目
    如何脱离SDK,使用DW5.5和phonegap以及JQMobile搭建开发环境
    JSON和JSONP有哪些区别,PhoneGap跨域请求如何实现
  • 原文地址:https://www.cnblogs.com/simon-sun/p/3412309.html
Copyright © 2011-2022 走看看