home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / Benchmark / Iterate.php next >
Encoding:
PHP Script  |  2006-04-07  |  4.5 KB  |  168 lines

  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.12 2006/02/17 16:29:44 toggg 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 © 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. class Benchmark_Iterate extends Benchmark_Timer {
  89.     /**
  90.      * Benchmarks a function or method.
  91.      *
  92.      * @access public
  93.      */
  94.     function run() {
  95.         $arguments     = func_get_args();
  96.         $iterations    = array_shift($arguments);
  97.         $function_name = array_shift($arguments);
  98.  
  99.         if (strstr($function_name, '::')) {
  100.           $function_name = explode('::', $function_name);
  101.           $objectmethod = $function_name[1];
  102.         }
  103.  
  104.         if (strstr($function_name, '->')) {
  105.             $function_name = explode('->', $function_name);
  106.             $objectname = $function_name[0];
  107.  
  108.             $object = $GLOBALS[$objectname];
  109.             $objectmethod = $function_name[1];
  110.  
  111.             for ($i = 1; $i <= $iterations; $i++) {
  112.                 $this->setMarker('start_' . $i);
  113.                 call_user_func_array(array($object, $function_name[1]), $arguments);
  114.                 $this->setMarker('end_' . $i);
  115.             }
  116.  
  117.             return(0);
  118.         }
  119.  
  120.         for ($i = 1; $i <= $iterations; $i++) {
  121.             $this->setMarker('start_' . $i);
  122.             call_user_func_array($function_name, $arguments);
  123.             $this->setMarker('end_' . $i);
  124.         }
  125.     }
  126.  
  127.     /**
  128.      * Returns benchmark result.
  129.      *
  130.      * $result[x           ] = execution time of iteration x
  131.      * $result['mean'      ] = mean execution time
  132.      * $result['iterations'] = number of iterations
  133.      *
  134.      * @return array
  135.      * @access public
  136.      */
  137.     function get($simple_output = false) {
  138.         $result = array();
  139.         $total  = 0;
  140.  
  141.         $iterations = count($this->markers)/2;
  142.  
  143.         for ($i = 1; $i <= $iterations; $i++) {
  144.             $time = $this->timeElapsed('start_'.$i , 'end_'.$i);
  145.  
  146.             if (extension_loaded('bcmath')) {
  147.                 $total = bcadd($total, $time, 6);
  148.             } else {
  149.                 $total = $total + $time;
  150.             }
  151.  
  152.             if (!$simple_output) {
  153.                 $result[$i] = $time;
  154.             }
  155.         }
  156.  
  157.         if (extension_loaded('bcmath')) {
  158.             $result['mean'] = bcdiv($total, $iterations, 6);
  159.         } else {
  160.             $result['mean'] = $total / $iterations;
  161.         }
  162.  
  163.         $result['iterations'] = $iterations;
  164.  
  165.         return $result;
  166.     }
  167. }
  168.