|
2 | 2 |
|
3 | 3 | $V = '1.0'; |
4 | 4 |
|
| 5 | +// Increase the multiplier if you want to benchmark longer |
| 6 | +$multiplier = 1.0; |
| 7 | + |
5 | 8 | /** @var array<string, callable> $benchmarks */ |
6 | 9 | // the benchmarks! |
7 | 10 | $benchmarks = [ |
8 | 11 | 'math' => function ($count = 200000) { |
| 12 | + global $multiplier; |
9 | 13 | $x = 0; |
10 | | - for ($i = 0; $i < $count; $i++) { |
| 14 | + for ($i = 0; $i < $count * $multiplier; $i++) { |
11 | 15 | $x += $i + $i; |
12 | 16 | $x += $i * $i; |
13 | 17 | $x += $i ** $i; |
|
52 | 56 | return $x; |
53 | 57 | }, |
54 | 58 | 'loops' => function ($count = 20000000) { |
55 | | - for ($i = 0; $i < $count; ++$i) { |
| 59 | + global $multiplier; |
| 60 | + for ($i = 0; $i < $count * $multiplier; ++$i) { |
56 | 61 | ; |
57 | 62 | } |
58 | 63 | $i = 0; |
59 | | - while ($i < $count) { |
| 64 | + while ($i < $count * $multiplier) { |
60 | 65 | ++$i; |
61 | 66 | } |
62 | 67 | return $i; |
63 | 68 | }, |
64 | 69 | 'ifelse' => function ($count = 10000000) { |
| 70 | + global $multiplier; |
65 | 71 | $a = 0; |
66 | 72 | $b = 0; |
67 | | - for ($i = 0; $i < $count; $i++) { |
| 73 | + for ($i = 0; $i < $count * $multiplier; $i++) { |
68 | 74 | $k = $i % 4; |
69 | 75 | if ($k === 0) { |
70 | 76 | $i; |
|
79 | 85 | return $a - $b; |
80 | 86 | }, |
81 | 87 | 'switch' => function ($count = 10000000) { |
| 88 | + global $multiplier; |
82 | 89 | $a = 0; |
83 | 90 | $b = 0; |
84 | | - for ($i = 0; $i < $count; $i++) { |
| 91 | + for ($i = 0; $i < $count * $multiplier; $i++) { |
85 | 92 | switch ($i % 4) { |
86 | 93 | case 0: |
87 | 94 | $i; |
|
99 | 106 | return $a - $b; |
100 | 107 | }, |
101 | 108 | 'strings' => function ($count = 50000) { |
| 109 | + global $multiplier; |
102 | 110 | $string = '<i>the</i> quick brown fox jumps over the lazy dog '; |
103 | | - for ($i = 0; $i < $count; $i++) { |
| 111 | + for ($i = 0; $i < $count * $multiplier; $i++) { |
104 | 112 | addslashes($string); |
105 | 113 | bin2hex($string); |
106 | 114 | chunk_split($string); |
|
135 | 143 | return $string; |
136 | 144 | }, |
137 | 145 | 'array' => function ($count = 50000) { |
| 146 | + global $multiplier; |
138 | 147 | $a = range(0, 100); |
139 | | - for ($i = 0; $i < $count; $i++) { |
| 148 | + for ($i = 0; $i < $count * $multiplier; $i++) { |
140 | 149 | array_keys($a); |
141 | 150 | array_values($a); |
142 | 151 | array_flip($a); |
|
153 | 162 | return $a; |
154 | 163 | }, |
155 | 164 | 'regex' => function ($count = 1000000) { |
156 | | - for ($i = 0; $i < $count; $i++) { |
| 165 | + global $multiplier; |
| 166 | + for ($i = 0; $i < $count * $multiplier; $i++) { |
157 | 167 | preg_match("#http[s]?://\w+[^\s\[\]\<]+#", 'this is a link to https://google.com which is a really popular site'); |
158 | 168 | preg_replace("#(^|\s)(http[s]?://\w+[^\s\[\]\<]+)#i", '\1<a href="\2">\2</a>', 'this is a link to https://google.com which is a really popular site'); |
159 | 169 | } |
160 | 170 | return $i; |
161 | 171 | }, |
162 | 172 | 'is_{type}' => function ($count = 2500000) { |
| 173 | + global $multiplier; |
163 | 174 | $o = new stdClass(); |
164 | | - for ($i = 0; $i < $count; $i++) { |
| 175 | + for ($i = 0; $i < $count * $multiplier; $i++) { |
165 | 176 | is_array([1]); |
166 | 177 | is_array('1'); |
167 | 178 | is_int(1); |
|
212 | 223 | $p('OPCache JIT', is_array($opStatus) && @$opStatus['jit']['enabled'] ? 'enabled' : 'disabled/unavailable'); |
213 | 224 | $p('PCRE JIT', ini_get('pcre.jit') ? 'enabled' : 'disabled'); |
214 | 225 | $p('XDebug extension', extension_loaded('xdebug') ? 'enabled' : 'disabled'); |
| 226 | +$p('Benchmark Multiplier', $multiplier); |
215 | 227 | $p('Started at', DateTime::createFromFormat('U.u', microtime(true))->format('d/m/Y H:i:s.v')); |
216 | 228 | $p('', '', '-'); |
217 | 229 |
|
|
0 commit comments