You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

419 lines
7.3 KiB

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