PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/test/subjects/benchmarks/zend/individual/ary3.php

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