summaryrefslogtreecommitdiff
path: root/src/bench
diff options
context:
space:
mode:
authorSebastien Blot2017-09-20 10:11:01 +0200
committerSebastien Blot2017-09-20 10:11:01 +0200
commit868f96c759b6650d88ff9f4fbc5c048302134248 (patch)
treec0de0af318bf77a8959164ef11aeeeb2b7bab294 /src/bench
Initial import
Diffstat (limited to 'src/bench')
-rw-r--r--src/bench/bench.php422
-rw-r--r--src/bench/micro_bench.php358
2 files changed, 780 insertions, 0 deletions
diff --git a/src/bench/bench.php b/src/bench/bench.php
new file mode 100644
index 0000000..5f77180
--- /dev/null
+++ b/src/bench/bench.php
@@ -0,0 +1,422 @@
1<?php
2if (function_exists("date_default_timezone_set")) {
3 date_default_timezone_set("UTC");
4}
5
6function simple() {
7 $a = 0;
8 for ($i = 0; $i < 1000000; $i++)
9 $a++;
10
11 $thisisanotherlongname = 0;
12 for ($thisisalongname = 0; $thisisalongname < 1000000; $thisisalongname++)
13 $thisisanotherlongname++;
14}
15
16/****/
17
18function simplecall() {
19 for ($i = 0; $i < 1000000; $i++)
20 strlen("hallo");
21}
22
23/****/
24
25function hallo($a) {
26}
27
28function simpleucall() {
29 for ($i = 0; $i < 1000000; $i++)
30 hallo("hallo");
31}
32
33/****/
34
35function simpleudcall() {
36 for ($i = 0; $i < 1000000; $i++)
37 hallo2("hallo");
38}
39
40function hallo2($a) {
41}
42
43/****/
44
45function mandel() {
46 $w1=50;
47 $h1=150;
48 $recen=-.45;
49 $imcen=0.0;
50 $r=0.7;
51 $s=0; $rec=0; $imc=0; $re=0; $im=0; $re2=0; $im2=0;
52 $x=0; $y=0; $w2=0; $h2=0; $color=0;
53 $s=2*$r/$w1;
54 $w2=40;
55 $h2=12;
56 for ($y=0 ; $y<=$w1; $y=$y+1) {
57 $imc=$s*($y-$h2)+$imcen;
58 for ($x=0 ; $x<=$h1; $x=$x+1) {
59 $rec=$s*($x-$w2)+$recen;
60 $re=$rec;
61 $im=$imc;
62 $color=1000;
63 $re2=$re*$re;
64 $im2=$im*$im;
65 while( ((($re2+$im2)<1000000) && $color>0)) {
66 $im=$re*$im*2+$imc;
67 $re=$re2-$im2+$rec;
68 $re2=$re*$re;
69 $im2=$im*$im;
70 $color=$color-1;
71 }
72 if ( $color==0 ) {
73 print "_";
74 } else {
75 print "#";
76 }
77 }
78 print "<br>";
79 flush();
80 }
81}
82
83/****/
84
85function mandel2() {
86 $b = " .:,;!/>)|&IH%*#";
87 //float r, i, z, Z, t, c, C;
88 for ($y=30; printf("\n"), $C = $y*0.1 - 1.5, $y--;){
89 for ($x=0; $c = $x*0.04 - 2, $z=0, $Z=0, $x++ < 75;){
90 for ($r=$c, $i=$C, $k=0; $t = $z*$z - $Z*$Z + $r, $Z = 2*$z*$Z + $i, $z=$t, $k<5000; $k++)
91 if ($z*$z + $Z*$Z > 500000) break;
92 echo $b[$k%16];
93 }
94 }
95}
96
97/****/
98
99function Ack($m, $n){
100 if($m == 0) return $n+1;
101 if($n == 0) return Ack($m-1, 1);
102 return Ack($m - 1, Ack($m, ($n - 1)));
103}
104
105function ackermann($n) {
106 $r = Ack(3,$n);
107 print "Ack(3,$n): $r\n";
108}
109
110/****/
111
112function ary($n) {
113 for ($i=0; $i<$n; $i++) {
114 $X[$i] = $i;
115 }
116 for ($i=$n-1; $i>=0; $i--) {
117 $Y[$i] = $X[$i];
118 }
119 $last = $n-1;
120 print "$Y[$last]\n";
121}
122
123/****/
124
125function ary2($n) {
126 for ($i=0; $i<$n;) {
127 $X[$i] = $i; ++$i;
128 $X[$i] = $i; ++$i;
129 $X[$i] = $i; ++$i;
130 $X[$i] = $i; ++$i;
131 $X[$i] = $i; ++$i;
132
133 $X[$i] = $i; ++$i;
134 $X[$i] = $i; ++$i;
135 $X[$i] = $i; ++$i;
136 $X[$i] = $i; ++$i;
137 $X[$i] = $i; ++$i;
138 }
139 for ($i=$n-1; $i>=0;) {
140 $Y[$i] = $X[$i]; --$i;
141 $Y[$i] = $X[$i]; --$i;
142 $Y[$i] = $X[$i]; --$i;
143 $Y[$i] = $X[$i]; --$i;
144 $Y[$i] = $X[$i]; --$i;
145
146 $Y[$i] = $X[$i]; --$i;
147 $Y[$i] = $X[$i]; --$i;
148 $Y[$i] = $X[$i]; --$i;
149 $Y[$i] = $X[$i]; --$i;
150 $Y[$i] = $X[$i]; --$i;
151 }
152 $last = $n-1;
153 print "$Y[$last]\n";
154}
155
156/****/
157
158function ary3($n) {
159 for ($i=0; $i<$n; $i++) {
160 $X[$i] = $i + 1;
161 $Y[$i] = 0;
162 }
163 for ($k=0; $k<1000; $k++) {
164 for ($i=$n-1; $i>=0; $i--) {
165 $Y[$i] += $X[$i];
166 }
167 }
168 $last = $n-1;
169 print "$Y[0] $Y[$last]\n";
170}
171
172/****/
173
174function fibo_r($n){
175 return(($n < 2) ? 1 : fibo_r($n - 2) + fibo_r($n - 1));
176}
177
178function fibo($n) {
179 $r = fibo_r($n);
180 print "$r\n";
181}
182
183/****/
184
185function hash1($n) {
186 for ($i = 1; $i <= $n; $i++) {
187 $X[dechex($i)] = $i;
188 }
189 $c = 0;
190 for ($i = $n; $i > 0; $i--) {
191 if ($X[dechex($i)]) { $c++; }
192 }
193 print "$c\n";
194}
195
196/****/
197
198function hash2($n) {
199 for ($i = 0; $i < $n; $i++) {
200 $hash1["foo_$i"] = $i;
201 $hash2["foo_$i"] = 0;
202 }
203 for ($i = $n; $i > 0; $i--) {
204 foreach($hash1 as $key => $value) $hash2[$key] += $value;
205 }
206 $first = "foo_0";
207 $last = "foo_".($n-1);
208 print "$hash1[$first] $hash1[$last] $hash2[$first] $hash2[$last]\n";
209}
210
211/****/
212
213function gen_random ($n) {
214 global $LAST;
215 return( ($n * ($LAST = ($LAST * IA + IC) % IM)) / IM );
216}
217
218function heapsort_r($n, &$ra) {
219 $l = ($n >> 1) + 1;
220 $ir = $n;
221
222 while (1) {
223 if ($l > 1) {
224 $rra = $ra[--$l];
225 } else {
226 $rra = $ra[$ir];
227 $ra[$ir] = $ra[1];
228 if (--$ir == 1) {
229 $ra[1] = $rra;
230 return;
231 }
232 }
233 $i = $l;
234 $j = $l << 1;
235 while ($j <= $ir) {
236 if (($j < $ir) && ($ra[$j] < $ra[$j+1])) {
237 $j++;
238 }
239 if ($rra < $ra[$j]) {
240 $ra[$i] = $ra[$j];
241 $j += ($i = $j);
242 } else {
243 $j = $ir + 1;
244 }
245 }
246 $ra[$i] = $rra;
247 }
248}
249
250function heapsort($N) {
251 global $LAST;
252
253 define("IM", 139968);
254 define("IA", 3877);
255 define("IC", 29573);
256
257 $LAST = 42;
258 for ($i=1; $i<=$N; $i++) {
259 $ary[$i] = gen_random(1);
260 }
261 heapsort_r($N, $ary);
262 printf("%.10f\n", $ary[$N]);
263}
264
265/****/
266
267function mkmatrix ($rows, $cols) {
268 $count = 1;
269 $mx = array();
270 for ($i=0; $i<$rows; $i++) {
271 for ($j=0; $j<$cols; $j++) {
272 $mx[$i][$j] = $count++;
273 }
274 }
275 return($mx);
276}
277
278function mmult ($rows, $cols, $m1, $m2) {
279 $m3 = array();
280 for ($i=0; $i<$rows; $i++) {
281 for ($j=0; $j<$cols; $j++) {
282 $x = 0;
283 for ($k=0; $k<$cols; $k++) {
284 $x += $m1[$i][$k] * $m2[$k][$j];
285 }
286 $m3[$i][$j] = $x;
287 }
288 }
289 return($m3);
290}
291
292function matrix($n) {
293 $SIZE = 30;
294 $m1 = mkmatrix($SIZE, $SIZE);
295 $m2 = mkmatrix($SIZE, $SIZE);
296 while ($n--) {
297 $mm = mmult($SIZE, $SIZE, $m1, $m2);
298 }
299 print "{$mm[0][0]} {$mm[2][3]} {$mm[3][2]} {$mm[4][4]}\n";
300}
301
302/****/
303
304function nestedloop($n) {
305 $x = 0;
306 for ($a=0; $a<$n; $a++)
307 for ($b=0; $b<$n; $b++)
308 for ($c=0; $c<$n; $c++)
309 for ($d=0; $d<$n; $d++)
310 for ($e=0; $e<$n; $e++)
311 for ($f=0; $f<$n; $f++)
312 $x++;
313 print "$x\n";
314}
315
316/****/
317
318function sieve($n) {
319 $count = 0;
320 while ($n-- > 0) {
321 $count = 0;
322 $flags = range (0,8192);
323 for ($i=2; $i<8193; $i++) {
324 if ($flags[$i] > 0) {
325 for ($k=$i+$i; $k <= 8192; $k+=$i) {
326 $flags[$k] = 0;
327 }
328 $count++;
329 }
330 }
331 }
332 print "Count: $count\n";
333}
334
335/****/
336
337function strcat($n) {
338 $str = "";
339 while ($n-- > 0) {
340 $str .= "hello\n";
341 }
342 $len = strlen($str);
343 print "$len\n";
344}
345
346/*****/
347
348function getmicrotime()
349{
350 $t = gettimeofday();
351 return ($t['sec'] + $t['usec'] / 1000000);
352}
353
354function start_test()
355{
356 ob_start();
357 return getmicrotime();
358}
359
360function end_test($start, $name)
361{
362 global $total;
363 $end = getmicrotime();
364 ob_end_clean();
365 $total += $end-$start;
366 $num = number_format($end-$start,3);
367 $pad = str_repeat(" ", 24-strlen($name)-strlen($num));
368
369 echo $name.$pad.$num."\n";
370 ob_start();
371 return getmicrotime();
372}
373
374function total()
375{
376 global $total;
377 $pad = str_repeat("-", 24);
378 echo $pad."\n";
379 $num = number_format($total,3);
380 $pad = str_repeat(" ", 24-strlen("Total")-strlen($num));
381 echo "Total".$pad.$num."\n";
382}
383
384$t0 = $t = start_test();
385simple();
386$t = end_test($t, "simple");
387simplecall();
388$t = end_test($t, "simplecall");
389simpleucall();
390$t = end_test($t, "simpleucall");
391simpleudcall();
392$t = end_test($t, "simpleudcall");
393mandel();
394$t = end_test($t, "mandel");
395mandel2();
396$t = end_test($t, "mandel2");
397ackermann(7);
398$t = end_test($t, "ackermann(7)");
399ary(50000);
400$t = end_test($t, "ary(50000)");
401ary2(50000);
402$t = end_test($t, "ary2(50000)");
403ary3(2000);
404$t = end_test($t, "ary3(2000)");
405fibo(30);
406$t = end_test($t, "fibo(30)");
407hash1(50000);
408$t = end_test($t, "hash1(50000)");
409hash2(500);
410$t = end_test($t, "hash2(500)");
411heapsort(20000);
412$t = end_test($t, "heapsort(20000)");
413matrix(20);
414$t = end_test($t, "matrix(20)");
415nestedloop(12);
416$t = end_test($t, "nestedloop(12)");
417sieve(30);
418$t = end_test($t, "sieve(30)");
419strcat(200000);
420$t = end_test($t, "strcat(200000)");
421total($t0, "Total");
422?>
diff --git a/src/bench/micro_bench.php b/src/bench/micro_bench.php
new file mode 100644
index 0000000..7052588
--- /dev/null
+++ b/src/bench/micro_bench.php
@@ -0,0 +1,358 @@
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 getmicrotime()
242{
243 $t = gettimeofday();
244 return ($t['sec'] + $t['usec'] / 1000000);
245}
246
247function start_test()
248{
249 ob_start();
250 return getmicrotime();
251}
252
253function end_test($start, $name, $overhead = null)
254{
255 global $total;
256 global $last_time;
257 $end = getmicrotime();
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 getmicrotime();
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");