php-internal-docs
8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
micro_bench.php
Go to the documentation of this file.
1
<?php
2
3
function
hallo
() {
4
}
5
6
function
simpleucall
(
$n
) {
7
for
($i = 0; $i <
$n
; $i++)
8
hallo
();
9
}
10
11
function
simpleudcall
(
$n
) {
12
for
($i = 0; $i <
$n
; $i++)
13
hallo2
();
14
}
15
16
function
hallo2
() {
17
}
18
19
function
simpleicall
(
$n
) {
20
for
($i = 0; $i <
$n
; $i++)
21
func_num_args
();
22
}
23
24
class
Foo
{
25
static
$a
= 0;
26
public
$b
= 0;
27
const
TEST
= 0;
28
29
static
function
read_static
(
$n
) {
30
for
($i = 0; $i <
$n
; ++$i) {
31
$x
= self::$a;
32
}
33
}
34
35
static
function
write_static
(
$n
) {
36
for
($i = 0; $i <
$n
; ++$i) {
37
self::$a = 0;
38
}
39
}
40
41
static
function
isset_static
(
$n
) {
42
for
($i = 0; $i <
$n
; ++$i) {
43
$x
= isset(self::$a);
44
}
45
}
46
47
static
function
empty_static
(
$n
) {
48
for
($i = 0; $i <
$n
; ++$i) {
49
$x
= empty(self::$a);
50
}
51
}
52
53
static
function
f
() {
54
}
55
56
static
function
call_static
(
$n
) {
57
for
($i = 0; $i <
$n
; ++$i) {
58
self::f
();
59
}
60
}
61
62
function
read_prop
(
$n
) {
63
for
($i = 0; $i <
$n
; ++$i) {
64
$x
=
$this->b
;
65
}
66
}
67
68
function
write_prop
(
$n
) {
69
for
($i = 0; $i <
$n
; ++$i) {
70
$this->b = 0;
71
}
72
}
73
74
function
assign_add_prop
(
$n
) {
75
for
($i = 0; $i <
$n
; ++$i) {
76
$this->b += 2;
77
}
78
}
79
80
function
pre_inc_prop
(
$n
) {
81
for
($i = 0; $i <
$n
; ++$i) {
82
++
$this->b
;
83
}
84
}
85
86
function
pre_dec_prop
(
$n
) {
87
for
($i = 0; $i <
$n
; ++$i) {
88
--
$this->b
;
89
}
90
}
91
92
function
post_inc_prop
(
$n
) {
93
for
($i = 0; $i <
$n
; ++$i) {
94
$this->b++;
95
}
96
}
97
98
function
post_dec_prop
(
$n
) {
99
for
($i = 0; $i <
$n
; ++$i) {
100
$this->b--;
101
}
102
}
103
104
function
isset_prop
(
$n
) {
105
for
($i = 0; $i <
$n
; ++$i) {
106
$x
= isset($this->b);
107
}
108
}
109
110
function
empty_prop
(
$n
) {
111
for
($i = 0; $i <
$n
; ++$i) {
112
$x
= empty($this->b);
113
}
114
}
115
116
function
g
() {
117
}
118
119
function
call
(
$n
) {
120
for
($i = 0; $i <
$n
; ++$i) {
121
$this->
g
();
122
}
123
}
124
125
function
read_const
(
$n
) {
126
for
($i = 0; $i <
$n
; ++$i) {
127
$x
= $this::TEST;
128
}
129
}
130
131
}
132
133
function
read_static
(
$n
) {
134
for
($i = 0; $i <
$n
; ++$i) {
135
$x
=
Foo::$a
;
136
}
137
}
138
139
function
write_static
(
$n
) {
140
for
($i = 0; $i <
$n
; ++$i) {
141
Foo::$a
= 0;
142
}
143
}
144
145
function
isset_static
(
$n
) {
146
for
($i = 0; $i <
$n
; ++$i) {
147
$x
= isset(
Foo::$a
);
148
}
149
}
150
151
function
empty_static
(
$n
) {
152
for
($i = 0; $i <
$n
; ++$i) {
153
$x
= empty(
Foo::$a
);
154
}
155
}
156
157
function
call_static
(
$n
) {
158
for
($i = 0; $i <
$n
; ++$i) {
159
Foo::f
();
160
}
161
}
162
163
function
create_object
(
$n
) {
164
for
($i = 0; $i <
$n
; ++$i) {
165
$x
=
new
Foo
();
166
}
167
}
168
169
define
(
'TEST'
,
null
);
170
171
function
read_const
(
$n
) {
172
for
($i = 0; $i <
$n
; ++$i) {
173
$x
=
TEST
;
174
}
175
}
176
177
function
read_auto_global
(
$n
) {
178
for
($i = 0; $i <
$n
; ++$i) {
179
$x
=
$_GET
;
180
}
181
}
182
183
$g_var
= 0;
184
185
function
read_global_var
(
$n
) {
186
for
($i = 0; $i <
$n
; ++$i) {
187
$x
= $GLOBALS[
'g_var'
];
188
}
189
}
190
191
function
read_hash
(
$n
) {
192
$hash = array(
'test'
=> 0);
193
for
($i = 0; $i <
$n
; ++$i) {
194
$x
= $hash[
'test'
];
195
}
196
}
197
198
function
read_str_offset
(
$n
) {
199
$str =
"test"
;
200
for
($i = 0; $i <
$n
; ++$i) {
201
$x
= $str[1];
202
}
203
}
204
205
function
issetor
(
$n
) {
206
$val = array(0,1,2,3,4,5,6,7,8,9);
207
for
($i = 0; $i <
$n
; ++$i) {
208
$x
= $val ?:
null
;
209
}
210
}
211
212
function
issetor2
(
$n
) {
213
$f =
false
; $j = 0;
214
for
($i = 0; $i <
$n
; ++$i) {
215
$x
= $f ?: $j + 1;
216
}
217
}
218
219
function
ternary
(
$n
) {
220
$val = array(0,1,2,3,4,5,6,7,8,9);
221
$f =
false
;
222
for
($i = 0; $i <
$n
; ++$i) {
223
$x
= $f ? null : $val;
224
}
225
}
226
227
function
ternary2
(
$n
) {
228
$f =
false
; $j = 0;
229
for
($i = 0; $i <
$n
; ++$i) {
230
$x
= $f ? $f : $j + 1;
231
}
232
}
233
234
/*****/
235
236
function
empty_loop
(
$n
) {
237
for
($i = 0; $i <
$n
; ++$i) {
238
}
239
}
240
241
function
gethrtime
()
242
{
243
$hrtime =
hrtime
();
244
return
(($hrtime[0]*1000000000 + $hrtime[1]) / 1000000000);
245
}
246
247
function
start_test
()
248
{
249
ob_start
();
250
return
gethrtime
();
251
}
252
253
function
end_test
($start,
$name
,
$overhead
=
null
)
254
{
255
global $total;
256
global $last_time;
257
$end =
gethrtime
();
258
ob_end_clean
();
259
$last_time = $end-$start;
260
$total += $last_time;
261
$num =
number_format
($last_time,3);
262
$pad =
str_repeat
(
" "
, 24-
strlen
(
$name
)-
strlen
($num));
263
if
(
is_null
(
$overhead
)) {
264
echo
$name
.$pad.$num.
"\n"
;
265
}
else
{
266
$num2 =
number_format
($last_time -
$overhead
,3);
267
echo
$name
.$pad.$num.
" "
.$num2.
"\n"
;
268
}
269
ob_start
();
270
return
gethrtime
();
271
}
272
273
function
total
()
274
{
275
global $total;
276
$pad =
str_repeat
(
"-"
, 24);
277
echo $pad.
"\n"
;
278
$num =
number_format
($total,3);
279
$pad =
str_repeat
(
" "
, 24-
strlen
(
"Total"
)-
strlen
($num));
280
echo
"Total"
.$pad.$num.
"\n"
;
281
}
282
283
const
N
= 5000000;
284
285
$t0
=
$t
=
start_test
();
286
empty_loop
(
N
);
287
$t
=
end_test
(
$t
,
'empty_loop'
);
288
$overhead
= $last_time;
289
simpleucall
(
N
);
290
$t
=
end_test
(
$t
,
'func()'
,
$overhead
);
291
simpleudcall
(
N
);
292
$t
=
end_test
(
$t
,
'undef_func()'
,
$overhead
);
293
simpleicall
(
N
);
294
$t
=
end_test
(
$t
,
'int_func()'
,
$overhead
);
295
Foo::read_static
(
N
);
296
$t
=
end_test
(
$t
,
'$x = self::$x'
,
$overhead
);
297
Foo::write_static
(
N
);
298
$t
=
end_test
(
$t
,
'self::$x = 0'
,
$overhead
);
299
Foo::isset_static
(
N
);
300
$t
=
end_test
(
$t
,
'isset(self::$x)'
,
$overhead
);
301
Foo::empty_static
(
N
);
302
$t
=
end_test
(
$t
,
'empty(self::$x)'
,
$overhead
);
303
read_static
(
N
);
304
$t
=
end_test
(
$t
,
'$x = Foo::$x'
,
$overhead
);
305
write_static
(
N
);
306
$t
=
end_test
(
$t
,
'Foo::$x = 0'
,
$overhead
);
307
isset_static
(
N
);
308
$t
=
end_test
(
$t
,
'isset(Foo::$x)'
,
$overhead
);
309
empty_static
(
N
);
310
$t
=
end_test
(
$t
,
'empty(Foo::$x)'
,
$overhead
);
311
Foo::call_static
(
N
);
312
$t
=
end_test
(
$t
,
'self::f()'
,
$overhead
);
313
call_static
(
N
);
314
$t
=
end_test
(
$t
,
'Foo::f()'
,
$overhead
);
315
$x
=
new
Foo
();
316
$x
->read_prop(
N
);
317
$t
=
end_test
(
$t
,
'$x = $this->x'
,
$overhead
);
318
$x
->write_prop(
N
);
319
$t
=
end_test
(
$t
,
'$this->x = 0'
,
$overhead
);
320
$x
->assign_add_prop(
N
);
321
$t
=
end_test
(
$t
,
'$this->x += 2'
,
$overhead
);
322
$x
->pre_inc_prop(
N
);
323
$t
=
end_test
(
$t
,
'++$this->x'
,
$overhead
);
324
$x
->pre_dec_prop(
N
);
325
$t
=
end_test
(
$t
,
'--$this->x'
,
$overhead
);
326
$x
->post_inc_prop(
N
);
327
$t
=
end_test
(
$t
,
'$this->x++'
,
$overhead
);
328
$x
->post_dec_prop(
N
);
329
$t
=
end_test
(
$t
,
'$this->x--'
,
$overhead
);
330
$x
->isset_prop(
N
);
331
$t
=
end_test
(
$t
,
'isset($this->x)'
,
$overhead
);
332
$x
->empty_prop(
N
);
333
$t
=
end_test
(
$t
,
'empty($this->x)'
,
$overhead
);
334
$x
->call(
N
);
335
$t
=
end_test
(
$t
,
'$this->f()'
,
$overhead
);
336
$x
->read_const(
N
);
337
$t
=
end_test
(
$t
,
'$x = Foo::TEST'
,
$overhead
);
338
create_object
(
N
);
339
$t
=
end_test
(
$t
,
'new Foo()'
,
$overhead
);
340
read_const
(
N
);
341
$t
=
end_test
(
$t
,
'$x = TEST'
,
$overhead
);
342
read_auto_global
(
N
);
343
$t
=
end_test
(
$t
,
'$x = $_GET'
,
$overhead
);
344
read_global_var
(
N
);
345
$t
=
end_test
(
$t
,
'$x = $GLOBALS[\'v\']'
,
$overhead
);
346
read_hash
(
N
);
347
$t
=
end_test
(
$t
,
'$x = $hash[\'v\']'
,
$overhead
);
348
read_str_offset
(
N
);
349
$t
=
end_test
(
$t
,
'$x = $str[0]'
,
$overhead
);
350
issetor
(
N
);
351
$t
=
end_test
(
$t
,
'$x = $a ?: null'
,
$overhead
);
352
issetor2
(
N
);
353
$t
=
end_test
(
$t
,
'$x = $f ?: tmp'
,
$overhead
);
354
ternary
(
N
);
355
$t
=
end_test
(
$t
,
'$x = $f ? $f : $a'
,
$overhead
);
356
ternary2
(
N
);
357
$t
=
end_test
(
$t
,
'$x = $f ? $f : tmp'
,
$overhead
);
358
total
(
$t0
,
"Total"
);
simpleucall
simpleucall()
Definition
bench.php:28
simpleudcall
simpleudcall()
Definition
bench.php:35
$t0
$t0
Definition
bench.php:384
$t
$t
Definition
bench.php:386
str_repeat
str_repeat(string $string, int $times)
Definition
basic_functions.stub.php:2571
is_null
is_null(mixed $value)
Definition
basic_functions.stub.php:3628
hrtime
hrtime(bool $as_number=false)
Definition
basic_functions.stub.php:2186
ob_start
ob_start($callback=null, int $chunk_size=0, int $flags=PHP_OUTPUT_HANDLER_STDFLAGS)
Definition
basic_functions.stub.php:1517
number_format
number_format(float $num, int $decimals=0, ?string $decimal_separator=".", ?string $thousands_separator=",")
Definition
basic_functions.stub.php:3264
ob_end_clean
ob_end_clean()
Definition
basic_functions.stub.php:1525
Foo
Definition
micro_bench.php:24
Foo\read_const
read_const($n)
Definition
micro_bench.php:125
Foo\write_prop
write_prop($n)
Definition
micro_bench.php:68
Foo\isset_static
static isset_static($n)
Definition
micro_bench.php:41
Foo\$b
$b
Definition
micro_bench.php:26
Foo\write_static
static write_static($n)
Definition
micro_bench.php:35
Foo\$a
static $a
Definition
micro_bench.php:25
Foo\isset_prop
isset_prop($n)
Definition
micro_bench.php:104
Foo\call
call($n)
Definition
micro_bench.php:119
Foo\TEST
const TEST
Definition
micro_bench.php:27
Foo\pre_dec_prop
pre_dec_prop($n)
Definition
micro_bench.php:86
Foo\read_static
static read_static($n)
Definition
micro_bench.php:29
Foo\read_prop
read_prop($n)
Definition
micro_bench.php:62
Foo\empty_static
static empty_static($n)
Definition
micro_bench.php:47
Foo\assign_add_prop
assign_add_prop($n)
Definition
micro_bench.php:74
Foo\post_dec_prop
post_dec_prop($n)
Definition
micro_bench.php:98
Foo\f
static f()
Definition
micro_bench.php:53
Foo\post_inc_prop
post_inc_prop($n)
Definition
micro_bench.php:92
Foo\pre_inc_prop
pre_inc_prop($n)
Definition
micro_bench.php:80
Foo\g
g()
Definition
micro_bench.php:116
Foo\empty_prop
empty_prop($n)
Definition
micro_bench.php:110
Foo\call_static
static call_static($n)
Definition
micro_bench.php:56
$n
$n
Definition
create-test.php:73
N
#define N
Definition
engine_mt19937.c:89
$name
$name
Definition
html_table_gen.php:352
read_auto_global
read_auto_global($n)
Definition
micro_bench.php:177
hallo2
hallo2()
Definition
micro_bench.php:16
start_test
start_test()
Definition
micro_bench.php:247
read_global_var
read_global_var($n)
Definition
micro_bench.php:185
ternary
ternary($n)
Definition
micro_bench.php:219
hallo
hallo()
Definition
micro_bench.php:3
read_str_offset
read_str_offset($n)
Definition
micro_bench.php:198
simpleicall
simpleicall($n)
Definition
micro_bench.php:19
gethrtime
gethrtime()
Definition
micro_bench.php:241
read_const
read_const($n)
Definition
micro_bench.php:171
ternary2
ternary2($n)
Definition
micro_bench.php:227
read_static
read_static($n)
Definition
micro_bench.php:133
TEST
const TEST
Definition
micro_bench.php:169
empty_static
empty_static($n)
Definition
micro_bench.php:151
$overhead
$overhead
Definition
micro_bench.php:288
total
total()
Definition
micro_bench.php:273
issetor2
issetor2($n)
Definition
micro_bench.php:212
issetor
issetor($n)
Definition
micro_bench.php:205
write_static
write_static($n)
Definition
micro_bench.php:139
empty_loop
empty_loop($n)
Definition
micro_bench.php:236
isset_static
isset_static($n)
Definition
micro_bench.php:145
call_static
call_static($n)
Definition
micro_bench.php:157
read_hash
read_hash($n)
Definition
micro_bench.php:191
end_test
end_test($start, $name, $overhead=null)
Definition
micro_bench.php:253
$g_var
$g_var
Definition
micro_bench.php:183
$x
$x
Definition
micro_bench.php:315
create_object
PdoFirebird_ce create_object
Definition
pdo_firebird.c:69
$_GET
$_GET
Definition
web-bootstrap.php:58
func_num_args
func_num_args()
Definition
zend_builtin_functions.stub.php:18
define
define(string $constant_name, mixed $value, bool $case_insensitive=false)
Definition
zend_builtin_functions.stub.php:41
strlen
strlen(string $string)
Definition
zend_builtin_functions.stub.php:25
Zend
micro_bench.php
Generated on Sat Aug 23 2025 01:46:13 for php-internal-docs by
1.13.2