summaryrefslogtreecommitdiff
path: root/buildscripts/Benchmark/Iterate.php
blob: f09dcb76ddd62c3a8b6aaa020847f4a0eb7b0692 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
//
// +------------------------------------------------------------------------+
// | PEAR :: Benchmark                                                      |
// +------------------------------------------------------------------------+
// | Copyright (c) 2001-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
// +------------------------------------------------------------------------+
// | This source file is subject to the New BSD license, That is bundled    |
// | with this package in the file LICENSE, and is available through        |
// | the world-wide-web at                                                  |
// | http://www.opensource.org/licenses/bsd-license.php                     |
// | If you did not receive a copy of the new BSDlicense and are unable     |
// | to obtain it through the world-wide-web, please send a note to         |
// | license@php.net so we can mail you a copy immediately.                 |
// +------------------------------------------------------------------------+
//
// $Id: Iterate.php,v 1.12 2006/02/17 16:29:44 toggg Exp $
//

require_once 'Benchmark/Timer.php';

/**
 * Provides timing and profiling information.
 *
 * Example 1
 *
 * <code>
 * <?php
 * require_once 'Benchmark/Iterate.php';
 *
 * $benchmark = new Benchmark_Iterate;
 *
 * function foo($string) {
 *     print $string . '<br>';
 * }
 *
 * $benchmark->run(100, 'foo', 'test');
 * $result = $benchmark->get();
 * ?>
 * </code>
 *
 * Example 2
 *
 * <code>
 * <?php
 * require_once 'Benchmark/Iterate.php';
 *
 * $benchmark = new Benchmark_Iterate;
 *
 * class MyClass {
 *     function foo($string) {
 *         print $string . '<br>';
 *     }
 * }
 *
 * $benchmark->run(100, 'myclass::foo', 'test');
 * $result = $benchmark->get();
 * ?>
 * </code>
 *
 * Example 3
 *
 * <code>
 * <?php
 * require_once 'Benchmark/Iterate.php';
 *
 * $benchmark = new Benchmark_Iterate;
 *
 * class MyClass {
 *     function foo($string) {
 *         print $string . '<br>';
 *     }
 * }
 *
 * $o = new MyClass();
 *
 * $benchmark->run(100, 'o->foo', 'test');
 * $result = $benchmark->get();
 * ?>
 * </code>
 *
 * @author    Sebastian Bergmann <sb@sebastian-bergmann.de>
 * @copyright Copyright &copy; 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
 * @license   http://www.php.net/license/3_0.txt The PHP License, Version 3.0
 * @category  Benchmarking
 * @package   Benchmark
 */
class Benchmark_Iterate extends Benchmark_Timer {
    /**
     * Benchmarks a function or method.
     *
     * @access public
     */
    function run() {
        $arguments     = func_get_args();
        $iterations    = array_shift($arguments);
        $function_name = array_shift($arguments);

        if (strstr($function_name, '::')) {
          $function_name = explode('::', $function_name);
          $objectmethod = $function_name[1];
        }

        if (strstr($function_name, '->')) {
            $function_name = explode('->', $function_name);
            $objectname = $function_name[0];

            $object = $GLOBALS[$objectname];
            $objectmethod = $function_name[1];

            for ($i = 1; $i <= $iterations; $i++) {
                $this->setMarker('start_' . $i);
                call_user_func_array(array($object, $function_name[1]), $arguments);
                $this->setMarker('end_' . $i);
            }

            return(0);
        }

        for ($i = 1; $i <= $iterations; $i++) {
            $this->setMarker('start_' . $i);
            call_user_func_array($function_name, $arguments);
            $this->setMarker('end_' . $i);
        }
    }

    /**
     * Returns benchmark result.
     *
     * $result[x           ] = execution time of iteration x
     * $result['mean'      ] = mean execution time
     * $result['iterations'] = number of iterations
     *
     * @return array
     * @access public
     */
    function get($simple_output = false) {
        $result = array();
        $total  = 0;

        $iterations = count($this->markers)/2;

        for ($i = 1; $i <= $iterations; $i++) {
            $time = $this->timeElapsed('start_'.$i , 'end_'.$i);

            if (extension_loaded('bcmath')) {
                $total = bcadd($total, $time, 6);
            } else {
                $total = $total + $time;
            }

            if (!$simple_output) {
                $result[$i] = $time;
            }
        }

        if (extension_loaded('bcmath')) {
            $result['mean'] = bcdiv($total, $iterations, 6);
        } else {
            $result['mean'] = $total / $iterations;
        }

        $result['iterations'] = $iterations;

        return $result;
    }
}