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 // +------------------------------------------------------------------------+
15 // $Id: Iterate.php,v 1.10 2005/06/11 12:09:20 toggg Exp $
18 require_once 'Benchmark/Timer.php';
21 * Provides timing and profiling information.
27 * require_once 'Benchmark/Iterate.php';
29 * $benchmark = new Benchmark_Iterate;
31 * function foo($string) {
32 * print $string . '<br>';
35 * $benchmark->run(100, 'foo', 'test');
36 * $result = $benchmark->get();
44 * require_once 'Benchmark/Iterate.php';
46 * $benchmark = new Benchmark_Iterate;
49 * function foo($string) {
50 * print $string . '<br>';
54 * $benchmark->run(100, 'myclass::foo', 'test');
55 * $result = $benchmark->get();
63 * require_once 'Benchmark/Iterate.php';
65 * $benchmark = new Benchmark_Iterate;
68 * function foo($string) {
69 * print $string . '<br>';
75 * $benchmark->run(100, 'o->foo', 'test');
76 * $result = $benchmark->get();
80 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
81 * @copyright Copyright © 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
86 class Benchmark_Iterate
extends Benchmark_Timer
{
88 * Benchmarks a function or method.
93 $arguments = func_get_args();
94 $iterations = array_shift($arguments);
95 $function_name = array_shift($arguments);
97 if (strstr($function_name, '::')) {
98 $function_name = explode('::', $function_name);
99 $objectmethod = $function_name[1];
102 if (strstr($function_name, '->')) {
103 $function_name = explode('->', $function_name);
104 $objectname = $function_name[0];
106 $object = $GLOBALS[$objectname];
107 $objectmethod = $function_name[1];
109 for ($i = 1; $i <= $iterations; $i++
) {
110 $this->setMarker('start_' . $i);
111 call_user_func_array(array($object, $function_name[1]), $arguments);
112 $this->setMarker('end_' . $i);
118 for ($i = 1; $i <= $iterations; $i++
) {
119 $this->setMarker('start_' . $i);
120 call_user_func_array($function_name, $arguments);
121 $this->setMarker('end_' . $i);
126 * Returns benchmark result.
128 * $result[x ] = execution time of iteration x
129 * $result['mean' ] = mean execution time
130 * $result['iterations'] = number of iterations
135 function get($simple_output = false) {
139 $iterations = count($this->markers
)/2;
141 for ($i = 1; $i <= $iterations; $i++
) {
142 $time = $this->timeElapsed('start_'.$i , 'end_'.$i);
144 if (extension_loaded('bcmath')) {
145 $total = bcadd($total, $time, 6);
147 $total = $total +
$time;
150 if (!$simple_output) {
155 if (extension_loaded('bcmath')) {
156 $result['mean'] = bcdiv($total, $iterations, 6);
158 $result['mean'] = $total / $iterations;
161 $result['iterations'] = $iterations;