PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/bench.php

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