Skip to content

Commit 2ed951a

Browse files
authored
Merge pull request #4 from H1p3ri0n/john/add-multiplier
Add a new Multiplier global variable to allow for increasing benchmark time.
2 parents 75e58ef + ee56a62 commit 2ed951a

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

bench.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
$V = '1.0';
44

5+
// Increase the multiplier if you want to benchmark longer
6+
$multiplier = 1.0;
7+
58
/** @var array<string, callable> $benchmarks */
69
// the benchmarks!
710
$benchmarks = [
811
'math' => function ($count = 200000) {
12+
global $multiplier;
913
$x = 0;
10-
for ($i = 0; $i < $count; $i++) {
14+
for ($i = 0; $i < $count * $multiplier; $i++) {
1115
$x += $i + $i;
1216
$x += $i * $i;
1317
$x += $i ** $i;
@@ -52,19 +56,21 @@
5256
return $x;
5357
},
5458
'loops' => function ($count = 20000000) {
55-
for ($i = 0; $i < $count; ++$i) {
59+
global $multiplier;
60+
for ($i = 0; $i < $count * $multiplier; ++$i) {
5661
;
5762
}
5863
$i = 0;
59-
while ($i < $count) {
64+
while ($i < $count * $multiplier) {
6065
++$i;
6166
}
6267
return $i;
6368
},
6469
'ifelse' => function ($count = 10000000) {
70+
global $multiplier;
6571
$a = 0;
6672
$b = 0;
67-
for ($i = 0; $i < $count; $i++) {
73+
for ($i = 0; $i < $count * $multiplier; $i++) {
6874
$k = $i % 4;
6975
if ($k === 0) {
7076
$i;
@@ -79,9 +85,10 @@
7985
return $a - $b;
8086
},
8187
'switch' => function ($count = 10000000) {
88+
global $multiplier;
8289
$a = 0;
8390
$b = 0;
84-
for ($i = 0; $i < $count; $i++) {
91+
for ($i = 0; $i < $count * $multiplier; $i++) {
8592
switch ($i % 4) {
8693
case 0:
8794
$i;
@@ -99,8 +106,9 @@
99106
return $a - $b;
100107
},
101108
'strings' => function ($count = 50000) {
109+
global $multiplier;
102110
$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++) {
104112
addslashes($string);
105113
bin2hex($string);
106114
chunk_split($string);
@@ -135,8 +143,9 @@
135143
return $string;
136144
},
137145
'array' => function ($count = 50000) {
146+
global $multiplier;
138147
$a = range(0, 100);
139-
for ($i = 0; $i < $count; $i++) {
148+
for ($i = 0; $i < $count * $multiplier; $i++) {
140149
array_keys($a);
141150
array_values($a);
142151
array_flip($a);
@@ -153,15 +162,17 @@
153162
return $a;
154163
},
155164
'regex' => function ($count = 1000000) {
156-
for ($i = 0; $i < $count; $i++) {
165+
global $multiplier;
166+
for ($i = 0; $i < $count * $multiplier; $i++) {
157167
preg_match("#http[s]?://\w+[^\s\[\]\<]+#", 'this is a link to https://google.com which is a really popular site');
158168
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');
159169
}
160170
return $i;
161171
},
162172
'is_{type}' => function ($count = 2500000) {
173+
global $multiplier;
163174
$o = new stdClass();
164-
for ($i = 0; $i < $count; $i++) {
175+
for ($i = 0; $i < $count * $multiplier; $i++) {
165176
is_array([1]);
166177
is_array('1');
167178
is_int(1);
@@ -212,6 +223,7 @@
212223
$p('OPCache JIT', is_array($opStatus) && @$opStatus['jit']['enabled'] ? 'enabled' : 'disabled/unavailable');
213224
$p('PCRE JIT', ini_get('pcre.jit') ? 'enabled' : 'disabled');
214225
$p('XDebug extension', extension_loaded('xdebug') ? 'enabled' : 'disabled');
226+
$p('Benchmark Multiplier', $multiplier);
215227
$p('Started at', DateTime::createFromFormat('U.u', microtime(true))->format('d/m/Y H:i:s.v'));
216228
$p('', '', '-');
217229

0 commit comments

Comments
 (0)