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

Source for file benchmark.php

Documentation is available at benchmark.php

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

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