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.6 2001/03/03 06:36:44 sbergmann Exp $ |
20 |
// |
21 |
|
22 |
require_once 'Benchmark/Timer.php'; |
23 |
|
24 |
/** |
25 |
* Benchmark::Benchmark_Iterate |
26 |
* |
27 |
* Purpose: |
28 |
* |
29 |
* Benchmarking |
30 |
* |
31 |
* Example: |
32 |
* |
33 |
* require_once "Benchmark/Iterate.php"; |
34 |
* $benchmark = new Benchmark_Iterate; |
35 |
* |
36 |
* function foo($string) |
37 |
* { |
38 |
* print $string."<br>"; |
39 |
* } |
40 |
* |
41 |
* $benchmark->run(100, 'foo', 'test'); |
42 |
* $result = $benchmark->get(); |
43 |
* |
44 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de> |
45 |
* @version $Revision: 1.6 $ |
46 |
* @access public |
47 |
*/ |
48 |
|
49 |
class Benchmark_Iterate extends Benchmark_Timer |
50 |
{ |
51 |
// {{{ run() |
52 |
|
53 |
/** |
54 |
* Benchmarks a function. |
55 |
* |
56 |
* @access public |
57 |
*/ |
58 |
|
59 |
function run() |
60 |
{ |
61 |
// get arguments |
62 |
$arguments = func_get_args(); |
63 |
$iterations = array_shift($arguments); |
64 |
$function_name = array_shift($arguments); |
65 |
|
66 |
// main loop |
67 |
for ($i = 1; $i <= $iterations; $i++) |
68 |
{ |
69 |
// set 'start' marker for current iteration |
70 |
$this->setMarker('start_'.$i); |
71 |
|
72 |
// call function to be benchmarked |
73 |
call_user_func_array($function_name, $arguments); |
74 |
|
75 |
// set 'end' marker for current iteration |
76 |
$this->setMarker('end_'.$i); |
77 |
} |
78 |
} |
79 |
|
80 |
// }}} |
81 |
// {{{ get() |
82 |
|
83 |
/** |
84 |
* Returns benchmark result. |
85 |
* |
86 |
* $result[x ] = execution time of iteration x |
87 |
* $result['mean' ] = mean execution time |
88 |
* $result['iterations'] = number of iterations |
89 |
* |
90 |
* @return array $result |
91 |
* @access public |
92 |
*/ |
93 |
|
94 |
function get() |
95 |
{ |
96 |
// init result array |
97 |
$result = array(); |
98 |
|
99 |
// init variable |
100 |
$total = 0; |
101 |
|
102 |
$iterations = count($this->markers)/2; |
103 |
|
104 |
// loop through iterations |
105 |
for ($i = 1; $i <= $iterations; $i++) |
106 |
{ |
107 |
// get elapsed time for current iteration |
108 |
$time = $this->timeElapsed('start_'.$i , 'end_'.$i); |
109 |
|
110 |
// sum up total time spent |
111 |
if (extension_loaded('bcmath')) { |
112 |
$total = bcadd($total, $time, 6); |
113 |
} else { |
114 |
$total = $total + $time; |
115 |
} |
116 |
|
117 |
// store time |
118 |
$result[$i] = $time; |
119 |
} |
120 |
|
121 |
// calculate and store mean time |
122 |
if (extension_loaded('bcmath')) { |
123 |
$result['mean'] = bcdiv($total, $iterations, 6); |
124 |
} else { |
125 |
$result['mean'] = $total / $iterations; |
126 |
} |
127 |
|
128 |
// store iterations |
129 |
$result['iterations'] = $iterations; |
130 |
|
131 |
// return result array |
132 |
return $result; |
133 |
} |
134 |
|
135 |
// }}} |
136 |
} |
137 |
?> |