Fixing content type ordering when content_type is not defined.
[akelos.git] / vendor / pear / Benchmark / Iterate.php
blob088d1b9d4556b320a6b43093b573ddab52d2dae3
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 // +------------------------------------------------------------------------+
15 // $Id: Iterate.php,v 1.10 2005/06/11 12:09:20 toggg Exp $
18 require_once 'Benchmark/Timer.php';
20 /**
21 * Provides timing and profiling information.
23 * Example 1
25 * <code>
26 * <?php
27 * require_once 'Benchmark/Iterate.php';
29 * $benchmark = new Benchmark_Iterate;
31 * function foo($string) {
32 * print $string . '<br>';
33 * }
35 * $benchmark->run(100, 'foo', 'test');
36 * $result = $benchmark->get();
37 * ?>
38 * </code>
40 * Example 2
42 * <code>
43 * <?php
44 * require_once 'Benchmark/Iterate.php';
46 * $benchmark = new Benchmark_Iterate;
48 * class MyClass {
49 * function foo($string) {
50 * print $string . '<br>';
51 * }
52 * }
54 * $benchmark->run(100, 'myclass::foo', 'test');
55 * $result = $benchmark->get();
56 * ?>
57 * </code>
59 * Example 3
61 * <code>
62 * <?php
63 * require_once 'Benchmark/Iterate.php';
65 * $benchmark = new Benchmark_Iterate;
67 * class MyClass {
68 * function foo($string) {
69 * print $string . '<br>';
70 * }
71 * }
73 * $o = new MyClass();
75 * $benchmark->run(100, 'o->foo', 'test');
76 * $result = $benchmark->get();
77 * ?>
78 * </code>
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
86 class Benchmark_Iterate extends Benchmark_Timer {
87 /**
88 * Benchmarks a function or method.
90 * @access public
92 function run() {
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);
115 return(0);
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
132 * @return array
133 * @access public
135 function get($simple_output = false) {
136 $result = array();
137 $total = 0;
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);
146 } else {
147 $total = $total + $time;
150 if (!$simple_output) {
151 $result[$i] = $time;
155 if (extension_loaded('bcmath')) {
156 $result['mean'] = bcdiv($total, $iterations, 6);
157 } else {
158 $result['mean'] = $total / $iterations;
161 $result['iterations'] = $iterations;
163 return $result;