xref: /PHP-8.4/Zend/micro_bench.php (revision 58b17906)
1<?php
2
3function hallo() {
4}
5
6function simpleucall($n) {
7  for ($i = 0; $i < $n; $i++)
8    hallo();
9}
10
11function simpleudcall($n) {
12  for ($i = 0; $i < $n; $i++)
13    hallo2();
14}
15
16function hallo2() {
17}
18
19function simpleicall($n) {
20  for ($i = 0; $i < $n; $i++)
21    func_num_args();
22}
23
24class 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
133function read_static($n) {
134    for ($i = 0; $i < $n; ++$i) {
135        $x = Foo::$a;
136    }
137}
138
139function write_static($n) {
140    for ($i = 0; $i < $n; ++$i) {
141        Foo::$a = 0;
142    }
143}
144
145function isset_static($n) {
146    for ($i = 0; $i < $n; ++$i) {
147        $x = isset(Foo::$a);
148    }
149}
150
151function empty_static($n) {
152    for ($i = 0; $i < $n; ++$i) {
153        $x = empty(Foo::$a);
154    }
155}
156
157function call_static($n) {
158    for ($i = 0; $i < $n; ++$i) {
159        Foo::f();
160    }
161}
162
163function create_object($n) {
164    for ($i = 0; $i < $n; ++$i) {
165        $x = new Foo();
166    }
167}
168
169define('TEST', null);
170
171function read_const($n) {
172    for ($i = 0; $i < $n; ++$i) {
173        $x = TEST;
174    }
175}
176
177function read_auto_global($n) {
178    for ($i = 0; $i < $n; ++$i) {
179        $x = $_GET;
180    }
181}
182
183$g_var = 0;
184
185function read_global_var($n) {
186    for ($i = 0; $i < $n; ++$i) {
187        $x = $GLOBALS['g_var'];
188    }
189}
190
191function read_hash($n) {
192    $hash = array('test' => 0);
193    for ($i = 0; $i < $n; ++$i) {
194        $x = $hash['test'];
195    }
196}
197
198function read_str_offset($n) {
199    $str = "test";
200    for ($i = 0; $i < $n; ++$i) {
201        $x = $str[1];
202    }
203}
204
205function 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
212function issetor2($n) {
213    $f = false; $j = 0;
214    for ($i = 0; $i < $n; ++$i) {
215        $x = $f ?: $j + 1;
216    }
217}
218
219function 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
227function 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
236function empty_loop($n) {
237    for ($i = 0; $i < $n; ++$i) {
238    }
239}
240
241function gethrtime()
242{
243  $hrtime = hrtime();
244  return (($hrtime[0]*1000000000 + $hrtime[1]) / 1000000000);
245}
246
247function start_test()
248{
249  ob_start();
250  return gethrtime();
251}
252
253function 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
273function 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
283const N = 5000000;
284
285$t0 = $t = start_test();
286empty_loop(N);
287$t = end_test($t, 'empty_loop');
288$overhead = $last_time;
289simpleucall(N);
290$t = end_test($t, 'func()', $overhead);
291simpleudcall(N);
292$t = end_test($t, 'undef_func()', $overhead);
293simpleicall(N);
294$t = end_test($t, 'int_func()', $overhead);
295Foo::read_static(N);
296$t = end_test($t, '$x = self::$x', $overhead);
297Foo::write_static(N);
298$t = end_test($t, 'self::$x = 0', $overhead);
299Foo::isset_static(N);
300$t = end_test($t, 'isset(self::$x)', $overhead);
301Foo::empty_static(N);
302$t = end_test($t, 'empty(self::$x)', $overhead);
303read_static(N);
304$t = end_test($t, '$x = Foo::$x', $overhead);
305write_static(N);
306$t = end_test($t, 'Foo::$x = 0', $overhead);
307isset_static(N);
308$t = end_test($t, 'isset(Foo::$x)', $overhead);
309empty_static(N);
310$t = end_test($t, 'empty(Foo::$x)', $overhead);
311Foo::call_static(N);
312$t = end_test($t, 'self::f()', $overhead);
313call_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);
338create_object(N);
339$t = end_test($t, 'new Foo()', $overhead);
340read_const(N);
341$t = end_test($t, '$x = TEST', $overhead);
342read_auto_global(N);
343$t = end_test($t, '$x = $_GET', $overhead);
344read_global_var(N);
345$t = end_test($t, '$x = $GLOBALS[\'v\']', $overhead);
346read_hash(N);
347$t = end_test($t, '$x = $hash[\'v\']', $overhead);
348read_str_offset(N);
349$t = end_test($t, '$x = $str[0]', $overhead);
350issetor(N);
351$t = end_test($t, '$x = $a ?: null', $overhead);
352issetor2(N);
353$t = end_test($t, '$x = $f ?: tmp', $overhead);
354ternary(N);
355$t = end_test($t, '$x = $f ? $f : $a', $overhead);
356ternary2(N);
357$t = end_test($t, '$x = $f ? $f : tmp', $overhead);
358total($t0, "Total");
359