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