File Source for benchmark.php

JAMA

# Source for file benchmark.php

Documentation is available at benchmark.php

1. <?php
3. error_reporting(E_ALL);
5. /**
6. * @package JAMA
7. */
9. require_once '../Matrix.php';
10. require_once 'Stats.php';
13. /**
14. * Example of use of Matrix Class, featuring magic squares.
15. */
16. class Benchmark {
17. public $stat;
20. /**
21. * Simple function to replicate PHP 5 behaviour
22. */
23. function microtime_float() {
24. list($usec, $sec) = explode(" ", microtime());
26. return ((float)$usec + (float)$sec);
27. } // function microtime_float()
30. function displayStats($times = null) {
31. $this-> stat->setData($times);
32. $stats = $this-> stat->calcFull();
34. echo '<table style="margin-left:32px;">';
35. echo '<tr><td style="text-align:right;"><b>n:</b><td style="text-align:right;">' . $stats['count'] . ' </td></tr>';
36. echo '<tr><td style="text-align:right;"><b>Mean:</b><td style="text-align:right;">' . $stats['mean'] . ' </td></tr>';
37. echo '<tr><td style="text-align:right;"><b>Min.:</b><td style="text-align:right;">' . $stats['min'] . ' </td></tr>';
38. echo '<tr><td style="text-align:right;"><b>Max.:</b><td style="text-align:right;">' . $stats['max'] . ' </td></tr>';
39. echo '<tr><td style="text-align:right;"><b>&sigma;:</b><td style="text-align:right;">' . $stats['stdev'] . ' </td></tr>';
40. echo '<tr><td style="text-align:right;"><b>Variance:</b><td style="text-align:right;">' . $stats['variance'] . ' </td></tr>';
41. echo '<tr><td style="text-align:right;"><b>Range:</b><td style="text-align:right;">' . $stats['range'] . ' </td></tr>';
42. echo '</table>';
44. return $stats;
45. } // function displayStats()
48. function runEig($n = 4, $t = 100) {
49. $times = array();
51. for ($i = 0; $i < $t; ++$i) {
52. $M = Matrix:: random($n, $n);
53. $start_time = $this-> microtime_float();
54. $E = new EigenvalueDecomposition($M);
55. $stop_time = $this-> microtime_float();
56. $times[] = $stop_time - $start_time;
57. }
59. return $times;
60. } // function runEig()
63. function runLU($n = 4, $t = 100) {
64. $times = array();
66. for ($i = 0; $i < $t; ++$i) {
67. $M = Matrix:: random($n, $n);
68. $start_time = $this-> microtime_float();
69. $E = new LUDecomposition($M);
70. $stop_time = $this-> microtime_float();
71. $times[] = $stop_time - $start_time;
72. }
74. return $times;
75. } // function runLU()
78. function runQR($n = 4, $t = 100) {
79. $times = array();
81. for ($i = 0; $i < $t; ++$i) {
82. $M = Matrix:: random($n, $n);
83. $start_time = $this-> microtime_float();
84. $E = new QRDecomposition($M);
85. $stop_time = $this-> microtime_float();
86. $times[] = $stop_time - $start_time;
87. }
89. return $times;
90. } // function runQR()
93. function runCholesky($n = 4, $t = 100) {
94. $times = array();
96. for ($i = 0; $i < $t; ++$i) {
97. $M = Matrix:: random($n, $n);
98. $start_time = $this-> microtime_float();
99. $E = new CholeskyDecomposition($M);
100. $stop_time = $this-> microtime_float();
101. $times[] = $stop_time - $start_time;
102. }
104. return $times;
105. } // function runCholesky()
108. function runSVD($n = 4, $t = 100) {
109. $times = array();
111. for ($i = 0; $i < $t; ++$i) {
112. $M = Matrix:: random($n, $n);
113. $start_time = $this-> microtime_float();
114. $E = new SingularValueDecomposition($M);
115. $stop_time = $this-> microtime_float();
116. $times[] = $stop_time - $start_time;
117. }
119. return $times;
120. } // function runSVD()
123. function run() {
124. $n = 8;
125. $t = 16;
126. $sum = 0;
127. echo "<b>Cholesky decomposition: $t random {$n}x{$n} matrices</b><br />";
128. $r = $this-> displayStats($this-> runCholesky($n, $t));
129. $sum += $r['mean'] * $n;
131. echo '<hr />';
133. echo "<b>Eigenvalue decomposition: $t random {$n}x{$n} matrices</b><br />";
134. $r = $this-> displayStats($this-> runEig($n, $t));
135. $sum += $r['mean'] * $n;
137. echo '<hr />';
139. echo "<b>LU decomposition: $t random {$n}x{$n} matrices</b><br />";
140. $r = $this-> displayStats($this-> runLU($n, $t));
141. $sum += $r['mean'] * $n;
143. echo '<hr />';
145. echo "<b>QR decomposition: $t random {$n}x{$n} matrices</b><br />";
146. $r = $this-> displayStats($this-> runQR($n, $t));
147. $sum += $r['mean'] * $n;
149. echo '<hr />';
151. echo "<b>Singular Value decomposition: $t random {$n}x{$n} matrices</b><br />";
152. $r = $this-> displayStats($this-> runSVD($n, $t));
153. $sum += $r['mean'] * $n;
155. return $sum;
156. } // function run()
159. public function __construct() {
160. $this-> stat = new Base();
161. } // function Benchmark()
163. } // class Benchmark (end MagicSquareExample)
166. $benchmark = new Benchmark();
168. switch($_REQUEST['decomposition']) {
169. case 'cholesky':
170. $m = array();
171. for ($i = 2; $i <= 8; $i *= 2) {
172. $t = 32 / $i;
173. echo "<b>Cholesky decomposition: $t random {$i}x{$i} matrices</b><br />";
174. $s = $benchmark-> displayStats($benchmark-> runCholesky($i, $t));
175. $m[$i] = $s['mean'];
176. echo "<br />";
177. }
178. echo '<pre>';
179. foreach($m as $x => $y) {
180. echo "$x\t" . 1000*$y . "\n";
181. }
182. echo '</pre>';
183. break;
184. case 'eigenvalue':
185. $m = array();
186. for ($i = 2; $i <= 8; $i *= 2) {
187. $t = 32 / $i;
188. echo "<b>Eigenvalue decomposition: $t random {$i}x{$i} matrices</b><br />";
189. $s = $benchmark-> displayStats($benchmark-> runEig($i, $t));
190. $m[$i] = $s['mean'];
191. echo "<br />";
192. }
193. echo '<pre>';
194. foreach($m as $x => $y) {
195. echo "$x\t" . 1000*$y . "\n";
196. }
197. echo '</pre>';
198. break;
199. case 'lu':
200. $m = array();
201. for ($i = 2; $i <= 8; $i *= 2) {
202. $t = 32 / $i;
203. echo "<b>LU decomposition: $t random {$i}x{$i} matrices</b><br />";
204. $s = $benchmark-> displayStats($benchmark-> runLU($i, $t));
205. $m[$i] = $s['mean'];
206. echo "<br />";
207. }
208. echo '<pre>';
209. foreach($m as $x => $y) {
210. echo "$x\t" . 1000*$y . "\n";
211. }
212. echo '</pre>';
213. break;
214. case 'qr':
215. $m = array();
216. for ($i = 2; $i <= 8; $i *= 2) {
217. $t = 32 / $i;
218. echo "<b>QR decomposition: $t random {$i}x{$i} matrices</b><br />";
219. $s = $benchmark-> displayStats($benchmark-> runQR($i, $t));
220. $m[$i] = $s['mean'];
221. echo "<br />";
222. }
223. echo '<pre>';
224. foreach($m as $x => $y) {
225. echo "$x\t" . 1000*$y . "\n";
226. }
227. echo '</pre>';
228. break;
229. case 'svd':
230. $m = array();
231. for($i = 2; $i <= 8; $i *= 2) {
232. $t = 32 / $i;
233. echo "<b>Singular value decomposition: $t random {$i}x{$i} matrices</b><br />";
234. $s = $benchmark-> displayStats($benchmark-> runSVD($i, $t));
235. $m[$i] = $s['mean'];
236. echo "<br />";
237. }
238. echo '<pre>';
239. foreach($m as $x => $y) {
240. echo "$x\t" . 1000*$y . "\n";
241. }
242. echo '</pre>';
243. break;
244. case 'all':
245. $s = $benchmark-> run();
246. print("<br /><b>Total<b>: {$s}s<br />");
247. break;
248. default:
249. ?>
250. <ul>
251. <li><a href="benchmark.php?decomposition=all">Complete Benchmark</a>
252. <ul>
253. <li><a href="benchmark.php?decomposition=cholesky">Cholesky</a></li>
254. <li><a href="benchmark.php?decomposition=eigenvalue">Eigenvalue</a></li>
255. <li><a href="benchmark.php?decomposition=lu">LU</a></li>
256. <li><a href="benchmark.php?decomposition=qr">QR</a></li>
257. <li><a href="benchmark.php?decomposition=svd">Singular Value</a></li>
258. </ul>
259. </li>
260. </ul>
261. <?php
262. break;
263. }

Documentation generated on Sun, 27 Feb 2011 16:28:28 -0800 by phpDocumentor 1.4.3