diff options
| author | Sebastien Blot | 2017-09-20 10:11:01 +0200 |
|---|---|---|
| committer | Sebastien Blot | 2017-09-20 10:11:01 +0200 |
| commit | 868f96c759b6650d88ff9f4fbc5c048302134248 (patch) | |
| tree | c0de0af318bf77a8959164ef11aeeeb2b7bab294 /src/bench/micro_bench.php | |
Initial import
Diffstat (limited to 'src/bench/micro_bench.php')
| -rw-r--r-- | src/bench/micro_bench.php | 358 |
1 files changed, 358 insertions, 0 deletions
diff --git a/src/bench/micro_bench.php b/src/bench/micro_bench.php new file mode 100644 index 0000000..7052588 --- /dev/null +++ b/src/bench/micro_bench.php | |||
| @@ -0,0 +1,358 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | function hallo() { | ||
| 4 | } | ||
| 5 | |||
| 6 | function simpleucall($n) { | ||
| 7 | for ($i = 0; $i < $n; $i++) | ||
| 8 | hallo(); | ||
| 9 | } | ||
| 10 | |||
| 11 | function simpleudcall($n) { | ||
| 12 | for ($i = 0; $i < $n; $i++) | ||
| 13 | hallo2(); | ||
| 14 | } | ||
| 15 | |||
| 16 | function hallo2() { | ||
| 17 | } | ||
| 18 | |||
| 19 | function simpleicall($n) { | ||
| 20 | for ($i = 0; $i < $n; $i++) | ||
| 21 | func_num_args(); | ||
| 22 | } | ||
| 23 | |||
| 24 | class Foo { | ||
| 25 | static $a = 0; | ||
| 26 | public $b = 0; | ||
| 27 | const TEST = 0; | ||
| 28 | |||
| 29 | static function read_static($n) { | ||
| 30 | for ($i = 0; $i < $n; ++$i) { | ||
| 31 | $x = self::$a; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | static function write_static($n) { | ||
| 36 | for ($i = 0; $i < $n; ++$i) { | ||
| 37 | self::$a = 0; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | static function isset_static($n) { | ||
| 42 | for ($i = 0; $i < $n; ++$i) { | ||
| 43 | $x = isset(self::$a); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | static function empty_static($n) { | ||
| 48 | for ($i = 0; $i < $n; ++$i) { | ||
| 49 | $x = empty(self::$a); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | static function f() { | ||
| 54 | } | ||
| 55 | |||
| 56 | static function call_static($n) { | ||
| 57 | for ($i = 0; $i < $n; ++$i) { | ||
| 58 | self::f(); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | function read_prop($n) { | ||
| 63 | for ($i = 0; $i < $n; ++$i) { | ||
| 64 | $x = $this->b; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | function write_prop($n) { | ||
| 69 | for ($i = 0; $i < $n; ++$i) { | ||
| 70 | $this->b = 0; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | function assign_add_prop($n) { | ||
| 75 | for ($i = 0; $i < $n; ++$i) { | ||
| 76 | $this->b += 2; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | function pre_inc_prop($n) { | ||
| 81 | for ($i = 0; $i < $n; ++$i) { | ||
| 82 | ++$this->b; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | function pre_dec_prop($n) { | ||
| 87 | for ($i = 0; $i < $n; ++$i) { | ||
| 88 | --$this->b; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | function post_inc_prop($n) { | ||
| 93 | for ($i = 0; $i < $n; ++$i) { | ||
| 94 | $this->b++; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | function post_dec_prop($n) { | ||
| 99 | for ($i = 0; $i < $n; ++$i) { | ||
| 100 | $this->b--; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | function isset_prop($n) { | ||
| 105 | for ($i = 0; $i < $n; ++$i) { | ||
| 106 | $x = isset($this->b); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | function empty_prop($n) { | ||
| 111 | for ($i = 0; $i < $n; ++$i) { | ||
| 112 | $x = empty($this->b); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | function g() { | ||
| 117 | } | ||
| 118 | |||
| 119 | function call($n) { | ||
| 120 | for ($i = 0; $i < $n; ++$i) { | ||
| 121 | $this->g(); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | function read_const($n) { | ||
| 126 | for ($i = 0; $i < $n; ++$i) { | ||
| 127 | $x = $this::TEST; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | } | ||
| 132 | |||
| 133 | function read_static($n) { | ||
| 134 | for ($i = 0; $i < $n; ++$i) { | ||
| 135 | $x = Foo::$a; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | function write_static($n) { | ||
| 140 | for ($i = 0; $i < $n; ++$i) { | ||
| 141 | Foo::$a = 0; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | function isset_static($n) { | ||
| 146 | for ($i = 0; $i < $n; ++$i) { | ||
| 147 | $x = isset(Foo::$a); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | function empty_static($n) { | ||
| 152 | for ($i = 0; $i < $n; ++$i) { | ||
| 153 | $x = empty(Foo::$a); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | function call_static($n) { | ||
| 158 | for ($i = 0; $i < $n; ++$i) { | ||
| 159 | Foo::f(); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | function create_object($n) { | ||
| 164 | for ($i = 0; $i < $n; ++$i) { | ||
| 165 | $x = new Foo(); | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | define('TEST', null); | ||
| 170 | |||
| 171 | function read_const($n) { | ||
| 172 | for ($i = 0; $i < $n; ++$i) { | ||
| 173 | $x = TEST; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | function read_auto_global($n) { | ||
| 178 | for ($i = 0; $i < $n; ++$i) { | ||
| 179 | $x = $_GET; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | $g_var = 0; | ||
| 184 | |||
| 185 | function read_global_var($n) { | ||
| 186 | for ($i = 0; $i < $n; ++$i) { | ||
| 187 | $x = $GLOBALS['g_var']; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | function read_hash($n) { | ||
| 192 | $hash = array('test' => 0); | ||
| 193 | for ($i = 0; $i < $n; ++$i) { | ||
| 194 | $x = $hash['test']; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | function read_str_offset($n) { | ||
| 199 | $str = "test"; | ||
| 200 | for ($i = 0; $i < $n; ++$i) { | ||
| 201 | $x = $str[1]; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | function issetor($n) { | ||
| 206 | $val = array(0,1,2,3,4,5,6,7,8,9); | ||
| 207 | for ($i = 0; $i < $n; ++$i) { | ||
| 208 | $x = $val ?: null; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | function issetor2($n) { | ||
| 213 | $f = false; $j = 0; | ||
| 214 | for ($i = 0; $i < $n; ++$i) { | ||
| 215 | $x = $f ?: $j + 1; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | function ternary($n) { | ||
| 220 | $val = array(0,1,2,3,4,5,6,7,8,9); | ||
| 221 | $f = false; | ||
| 222 | for ($i = 0; $i < $n; ++$i) { | ||
| 223 | $x = $f ? null : $val; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | function ternary2($n) { | ||
| 228 | $f = false; $j = 0; | ||
| 229 | for ($i = 0; $i < $n; ++$i) { | ||
| 230 | $x = $f ? $f : $j + 1; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | /*****/ | ||
| 235 | |||
| 236 | function empty_loop($n) { | ||
| 237 | for ($i = 0; $i < $n; ++$i) { | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | function getmicrotime() | ||
| 242 | { | ||
| 243 | $t = gettimeofday(); | ||
| 244 | return ($t['sec'] + $t['usec'] / 1000000); | ||
| 245 | } | ||
| 246 | |||
| 247 | function start_test() | ||
| 248 | { | ||
| 249 | ob_start(); | ||
| 250 | return getmicrotime(); | ||
| 251 | } | ||
| 252 | |||
| 253 | function end_test($start, $name, $overhead = null) | ||
| 254 | { | ||
| 255 | global $total; | ||
| 256 | global $last_time; | ||
| 257 | $end = getmicrotime(); | ||
| 258 | ob_end_clean(); | ||
| 259 | $last_time = $end-$start; | ||
| 260 | $total += $last_time; | ||
| 261 | $num = number_format($last_time,3); | ||
| 262 | $pad = str_repeat(" ", 24-strlen($name)-strlen($num)); | ||
| 263 | if (is_null($overhead)) { | ||
| 264 | echo $name.$pad.$num."\n"; | ||
| 265 | } else { | ||
| 266 | $num2 = number_format($last_time - $overhead,3); | ||
| 267 | echo $name.$pad.$num." ".$num2."\n"; | ||
| 268 | } | ||
| 269 | ob_start(); | ||
| 270 | return getmicrotime(); | ||
| 271 | } | ||
| 272 | |||
| 273 | function total() | ||
| 274 | { | ||
| 275 | global $total; | ||
| 276 | $pad = str_repeat("-", 24); | ||
| 277 | echo $pad."\n"; | ||
| 278 | $num = number_format($total,3); | ||
| 279 | $pad = str_repeat(" ", 24-strlen("Total")-strlen($num)); | ||
| 280 | echo "Total".$pad.$num."\n"; | ||
| 281 | } | ||
| 282 | |||
| 283 | const N = 5000000; | ||
| 284 | |||
| 285 | $t0 = $t = start_test(); | ||
| 286 | empty_loop(N); | ||
| 287 | $t = end_test($t, 'empty_loop'); | ||
| 288 | $overhead = $last_time; | ||
| 289 | simpleucall(N); | ||
| 290 | $t = end_test($t, 'func()', $overhead); | ||
| 291 | simpleudcall(N); | ||
| 292 | $t = end_test($t, 'undef_func()', $overhead); | ||
| 293 | simpleicall(N); | ||
| 294 | $t = end_test($t, 'int_func()', $overhead); | ||
| 295 | Foo::read_static(N); | ||
| 296 | $t = end_test($t, '$x = self::$x', $overhead); | ||
| 297 | Foo::write_static(N); | ||
| 298 | $t = end_test($t, 'self::$x = 0', $overhead); | ||
| 299 | Foo::isset_static(N); | ||
| 300 | $t = end_test($t, 'isset(self::$x)', $overhead); | ||
| 301 | Foo::empty_static(N); | ||
| 302 | $t = end_test($t, 'empty(self::$x)', $overhead); | ||
| 303 | read_static(N); | ||
| 304 | $t = end_test($t, '$x = Foo::$x', $overhead); | ||
| 305 | write_static(N); | ||
| 306 | $t = end_test($t, 'Foo::$x = 0', $overhead); | ||
| 307 | isset_static(N); | ||
| 308 | $t = end_test($t, 'isset(Foo::$x)', $overhead); | ||
| 309 | empty_static(N); | ||
| 310 | $t = end_test($t, 'empty(Foo::$x)', $overhead); | ||
| 311 | Foo::call_static(N); | ||
| 312 | $t = end_test($t, 'self::f()', $overhead); | ||
| 313 | call_static(N); | ||
| 314 | $t = end_test($t, 'Foo::f()', $overhead); | ||
| 315 | $x = new Foo(); | ||
| 316 | $x->read_prop(N); | ||
| 317 | $t = end_test($t, '$x = $this->x', $overhead); | ||
| 318 | $x->write_prop(N); | ||
| 319 | $t = end_test($t, '$this->x = 0', $overhead); | ||
| 320 | $x->assign_add_prop(N); | ||
| 321 | $t = end_test($t, '$this->x += 2', $overhead); | ||
| 322 | $x->pre_inc_prop(N); | ||
| 323 | $t = end_test($t, '++$this->x', $overhead); | ||
| 324 | $x->pre_dec_prop(N); | ||
| 325 | $t = end_test($t, '--$this->x', $overhead); | ||
| 326 | $x->post_inc_prop(N); | ||
| 327 | $t = end_test($t, '$this->x++', $overhead); | ||
| 328 | $x->post_dec_prop(N); | ||
| 329 | $t = end_test($t, '$this->x--', $overhead); | ||
| 330 | $x->isset_prop(N); | ||
| 331 | $t = end_test($t, 'isset($this->x)', $overhead); | ||
| 332 | $x->empty_prop(N); | ||
| 333 | $t = end_test($t, 'empty($this->x)', $overhead); | ||
| 334 | $x->call(N); | ||
| 335 | $t = end_test($t, '$this->f()', $overhead); | ||
| 336 | $x->read_const(N); | ||
| 337 | $t = end_test($t, '$x = Foo::TEST', $overhead); | ||
| 338 | create_object(N); | ||
| 339 | $t = end_test($t, 'new Foo()', $overhead); | ||
| 340 | read_const(N); | ||
| 341 | $t = end_test($t, '$x = TEST', $overhead); | ||
| 342 | read_auto_global(N); | ||
| 343 | $t = end_test($t, '$x = $_GET', $overhead); | ||
| 344 | read_global_var(N); | ||
| 345 | $t = end_test($t, '$x = $GLOBALS[\'v\']', $overhead); | ||
| 346 | read_hash(N); | ||
| 347 | $t = end_test($t, '$x = $hash[\'v\']', $overhead); | ||
| 348 | read_str_offset(N); | ||
| 349 | $t = end_test($t, '$x = $str[0]', $overhead); | ||
| 350 | issetor(N); | ||
| 351 | $t = end_test($t, '$x = $a ?: null', $overhead); | ||
| 352 | issetor2(N); | ||
| 353 | $t = end_test($t, '$x = $f ?: tmp', $overhead); | ||
| 354 | ternary(N); | ||
| 355 | $t = end_test($t, '$x = $f ? $f : $a', $overhead); | ||
| 356 | ternary2(N); | ||
| 357 | $t = end_test($t, '$x = $f ? $f : tmp', $overhead); | ||
| 358 | total($t0, "Total"); | ||
