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

Documentation generated on Mon, 11 Mar 2019 14:18:40 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.