JAMA
[ class tree: JAMA ] [ index: JAMA ] [ all elements ]

Source for file benchmark.php

Documentation is available at benchmark.php

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

Documentation generated on Mon, 05 Jan 2009 20:36:26 +0100 by phpDocumentor 1.4.1