bench.php 7.5 KB

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