home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Vyzkuste / phptriad / phptriad2-2-1.exe / php / pear / Benchmark / Iterate.php next >
Encoding:
PHP Script  |  2001-11-13  |  4.6 KB  |  160 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2001 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | 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. // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de>               |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Iterate.php,v 1.9.2.3 2001/11/13 01:26:40 ssb Exp $
  20. //
  21.  
  22. require_once 'Benchmark/Timer.php';
  23.  
  24. /**
  25. * Benchmark::Benchmark_Iterate
  26. * Purpose:
  27. *     Benchmarking
  28. * Example:
  29. *   a) 
  30. *     require_once 'Benchmark/Iterate.php';
  31. *     $benchmark = new Benchmark_Iterate;
  32. *     function foo($string) {
  33. *         print $string . '<br>';
  34. *     }
  35. *     $benchmark->run(100, 'foo', 'test');
  36. *     $result = $benchmark->get();
  37. *
  38. *   b)
  39. *     require_once 'Benchmark/Iterate.php';
  40. *     $benchmark = new Benchmark_Iterate;
  41. *     class myclass{
  42. *
  43. *       function foo($string) {
  44. *             print $string . '<br>';
  45. *       }
  46. *     }
  47. *     $benchmark->run(100, 'myclass::foo', 'test');
  48. *     $result = $benchmark->get();
  49. *
  50. *   c)
  51. *     require_once 'Benchmark/Iterate.php';
  52. *     $benchmark = new Benchmark_Iterate;
  53. *     class myclass{
  54. *
  55. *       function foo($string) {
  56. *             print $string . '<br>';
  57. *       }
  58. *     }
  59. *
  60. *     $myobj = new myclass();
  61. *     $benchmark->run(100, 'myobj->foo', 'test');
  62. *     $result = $benchmark->get();
  63. *
  64. * @author   Sebastian Bergmann <sb@sebastian-bergmann.de>
  65. * @version  $Revision: 1.9.2.3 $
  66. * @access   public
  67. */
  68.  
  69. class Benchmark_Iterate extends Benchmark_Timer {
  70.     /**
  71.     * Benchmarks a function.
  72.     *
  73.     * @access public
  74.     */
  75.  
  76.     function run() {
  77.         $arguments     = func_get_args();
  78.  
  79.         $iterations    = array_shift($arguments);
  80.         $function_name = array_shift($arguments);
  81.     
  82.         if (strstr($function_name, '::')) {
  83.           $function_name = explode('::', $function_name);
  84.           $objectmethod = $function_name[1];
  85.         }
  86.  
  87.         // If we're calling a method on an object use call_user_method
  88.         if (strstr($function_name, '->')) {
  89.             $function_name = explode('->', $function_name);
  90.             $objectname = $function_name[0];
  91.  
  92.             global ${$objectname};
  93.             $objectmethod = $function_name[1];
  94.  
  95.             for ($i = 1; $i <= $iterations; $i++) {
  96.                 $this->setMarker('start_' . $i);
  97.                 call_user_method_array($function_name[1], ${$objectname}, $arguments);
  98.                 $this->setMarker('end_' . $i);
  99.             }
  100.             return(0);
  101.         }
  102.  
  103.         for ($i = 1; $i <= $iterations; $i++) {
  104.             $this->setMarker('start_' . $i);
  105.             call_user_func_array($function_name, $arguments);
  106.             $this->setMarker('end_' . $i);
  107.         }
  108.     }
  109.  
  110.     /**
  111.     * Returns benchmark result.
  112.     *
  113.     * $result[x           ] = execution time of iteration x
  114.     * $result['mean'      ] = mean execution time
  115.     * $result['iterations'] = number of iterations
  116.     *
  117.     * @return array $result
  118.     * @access public
  119.     */
  120.  
  121.     function get() {
  122.         $result = array();
  123.         $total  = 0;
  124.  
  125.         $iterations = count($this->markers)/2;
  126.  
  127.         for ($i = 1; $i <= $iterations; $i++) {
  128.             $time = $this->timeElapsed('start_'.$i , 'end_'.$i);
  129.  
  130.             if (extension_loaded('bcmath')) {
  131.                 $total = bcadd($total, $time, 6);
  132.             } else {
  133.                 $total = $total + $time;
  134.             }
  135.             
  136.             $result[$i] = $time;
  137.         }
  138.  
  139.         if (extension_loaded('bcmath')) {
  140.             $result['mean'] = bcdiv($total, $iterations, 6);
  141.         } else {
  142.             $result['mean'] = $total / $iterations;
  143.         }
  144.  
  145.         $result['iterations'] = $iterations;
  146.  
  147.         return $result;
  148.     }
  149. }
  150. ?>
  151.