php 类静态变量 和 常量消耗内存及时间对比

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

其中:

常量消耗:101.1739毫秒

变量消耗:2039.7689毫秒

静态变量消耗:4084.8911毫秒



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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;


鼎云博客
  • 最新评论
  • 总共0条评论