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

Source for file Iterate.php

Documentation is available at Iterate.php

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: Benchmark                                                      |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2001-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
  7. // +------------------------------------------------------------------------+
  8. // | This source file is subject to the New BSD license, That is bundled    |
  9. // | with this package in the file LICENSE, and is available through        |
  10. // | the world-wide-web at                                                  |
  11. // | http://www.opensource.org/licenses/bsd-license.php                     |
  12. // | If you did not receive a copy of the new BSDlicense and are unable     |
  13. // | to obtain it through the world-wide-web, please send a note to         |
  14. // | license@php.net so we can mail you a copy immediately.                 |
  15. // +------------------------------------------------------------------------+
  16. //
  17. // $Id: Iterate.php,v 1.1 2007/05/24 05:17:56 anant Exp $
  18. //
  19.  
  20. require_once 'Benchmark/Timer.php';
  21.  
  22. /**
  23.  * Provides timing and profiling information.
  24.  *
  25.  * Example 1
  26.  *
  27.  * <code>
  28.  * <?php
  29.  * require_once 'Benchmark/Iterate.php';
  30.  *
  31.  * $benchmark = new Benchmark_Iterate;
  32.  *
  33.  * function foo($string) {
  34.  *     print $string . '<br>';
  35.  * }
  36.  *
  37.  * $benchmark->run(100, 'foo', 'test');
  38.  * $result = $benchmark->get();
  39.  * ?>
  40.  * </code>
  41.  *
  42.  * Example 2
  43.  *
  44.  * <code>
  45.  * <?php
  46.  * require_once 'Benchmark/Iterate.php';
  47.  *
  48.  * $benchmark = new Benchmark_Iterate;
  49.  *
  50.  * class MyClass {
  51.  *     function foo($string) {
  52.  *         print $string . '<br>';
  53.  *     }
  54.  * }
  55.  *
  56.  * $benchmark->run(100, 'myclass::foo', 'test');
  57.  * $result = $benchmark->get();
  58.  * ?>
  59.  * </code>
  60.  *
  61.  * Example 3
  62.  *
  63.  * <code>
  64.  * <?php
  65.  * require_once 'Benchmark/Iterate.php';
  66.  *
  67.  * $benchmark = new Benchmark_Iterate;
  68.  *
  69.  * class MyClass {
  70.  *     function foo($string) {
  71.  *         print $string . '<br>';
  72.  *     }
  73.  * }
  74.  *
  75.  * $o = new MyClass();
  76.  *
  77.  * $benchmark->run(100, 'o->foo', 'test');
  78.  * $result = $benchmark->get();
  79.  * ?>
  80.  * </code>
  81.  *
  82.  * @author    Sebastian Bergmann <sb@sebastian-bergmann.de>
  83.  * @copyright Copyright &copy; 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
  84.  * @license   http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  85.  * @category  Benchmarking
  86.  * @package   Benchmark
  87.  */
  88.     /**
  89.      * Benchmarks a function or method.
  90.      *
  91.      * @access public
  92.      */
  93.     function run({
  94.         $arguments     func_get_args();
  95.         $iterations    array_shift($arguments);
  96.         $function_name array_shift($arguments);
  97.  
  98.         if (strstr($function_name'::')) {
  99.           $function_name explode('::'$function_name);
  100.           $objectmethod $function_name[1];
  101.         }
  102.  
  103.         if (strstr($function_name'->')) {
  104.             $function_name explode('->'$function_name);
  105.             $objectname $function_name[0];
  106.  
  107.             $object $GLOBALS[$objectname];
  108.             $objectmethod $function_name[1];
  109.  
  110.             for ($i = 1; $i <= $iterations$i++{
  111.                 $this->setMarker('start_' $i);
  112.                 call_user_func_array(array($object$function_name[1])$arguments);
  113.                 $this->setMarker('end_' $i);
  114.             }
  115.  
  116.             return(0);
  117.         }
  118.  
  119.         for ($i = 1; $i <= $iterations$i++{
  120.             $this->setMarker('start_' $i);
  121.             call_user_func_array($function_name$arguments);
  122.             $this->setMarker('end_' $i);
  123.         }
  124.     }
  125.  
  126.     /**
  127.      * Returns benchmark result.
  128.      *
  129.      * $result[x           ] = execution time of iteration x
  130.      * $result['mean'      ] = mean execution time
  131.      * $result['iterations'] = number of iterations
  132.      *
  133.      * @return array 
  134.      * @access public
  135.      */
  136.     function get($simple_output = false{
  137.         $result = array();
  138.         $total  = 0;
  139.  
  140.         $iterations count($this->markers)/2;
  141.  
  142.         for ($i = 1; $i <= $iterations$i++{
  143.             $time $this->timeElapsed('start_'.$i 'end_'.$i);
  144.  
  145.             if (extension_loaded('bcmath')) {
  146.                 $total = bcadd($total$time6);
  147.             else {
  148.                 $total $total $time;
  149.             }
  150.  
  151.             if (!$simple_output{
  152.                 $result[$i$time;
  153.             }
  154.         }
  155.  
  156.         if (extension_loaded('bcmath')) {
  157.             $result['mean'= bcdiv($total$iterations6);
  158.         else {
  159.             $result['mean'$total $iterations;
  160.         }
  161.  
  162.         $result['iterations'$iterations;
  163.  
  164.         return $result;
  165.     }
  166. }

Documentation generated on Fri, 22 Jun 2007 21:30:02 -0400 by phpDocumentor 1.3.2. PEAR Logo Copyright © PHP Group 2004.