Skip to content

Commit

Permalink
Merge pull request #4 from H1p3ri0n/john/add-multiplier
Browse files Browse the repository at this point in the history
Add a new Multiplier global variable to allow for increasing benchmark time.
  • Loading branch information
sergix44 authored Nov 28, 2023
2 parents 75e58ef + ee56a62 commit 2ed951a
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions bench.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

$V = '1.0';

// Increase the multiplier if you want to benchmark longer
$multiplier = 1.0;

/** @var array<string, callable> $benchmarks */
// the benchmarks!
$benchmarks = [
'math' => function ($count = 200000) {
global $multiplier;
$x = 0;
for ($i = 0; $i < $count; $i++) {
for ($i = 0; $i < $count * $multiplier; $i++) {
$x += $i + $i;
$x += $i * $i;
$x += $i ** $i;
Expand Down Expand Up @@ -52,19 +56,21 @@
return $x;
},
'loops' => function ($count = 20000000) {
for ($i = 0; $i < $count; ++$i) {
global $multiplier;
for ($i = 0; $i < $count * $multiplier; ++$i) {
;
}
$i = 0;
while ($i < $count) {
while ($i < $count * $multiplier) {
++$i;
}
return $i;
},
'ifelse' => function ($count = 10000000) {
global $multiplier;
$a = 0;
$b = 0;
for ($i = 0; $i < $count; $i++) {
for ($i = 0; $i < $count * $multiplier; $i++) {
$k = $i % 4;
if ($k === 0) {
$i;
Expand All @@ -79,9 +85,10 @@
return $a - $b;
},
'switch' => function ($count = 10000000) {
global $multiplier;
$a = 0;
$b = 0;
for ($i = 0; $i < $count; $i++) {
for ($i = 0; $i < $count * $multiplier; $i++) {
switch ($i % 4) {
case 0:
$i;
Expand All @@ -99,8 +106,9 @@
return $a - $b;
},
'strings' => function ($count = 50000) {
global $multiplier;
$string = '<i>the</i> quick brown fox jumps over the lazy dog ';
for ($i = 0; $i < $count; $i++) {
for ($i = 0; $i < $count * $multiplier; $i++) {
addslashes($string);
bin2hex($string);
chunk_split($string);
Expand Down Expand Up @@ -135,8 +143,9 @@
return $string;
},
'array' => function ($count = 50000) {
global $multiplier;
$a = range(0, 100);
for ($i = 0; $i < $count; $i++) {
for ($i = 0; $i < $count * $multiplier; $i++) {
array_keys($a);
array_values($a);
array_flip($a);
Expand All @@ -153,15 +162,17 @@
return $a;
},
'regex' => function ($count = 1000000) {
for ($i = 0; $i < $count; $i++) {
global $multiplier;
for ($i = 0; $i < $count * $multiplier; $i++) {
preg_match("#http[s]?://\w+[^\s\[\]\<]+#", 'this is a link to https://google.com which is a really popular site');
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');
}
return $i;
},
'is_{type}' => function ($count = 2500000) {
global $multiplier;
$o = new stdClass();
for ($i = 0; $i < $count; $i++) {
for ($i = 0; $i < $count * $multiplier; $i++) {
is_array([1]);
is_array('1');
is_int(1);
Expand Down Expand Up @@ -212,6 +223,7 @@
$p('OPCache JIT', is_array($opStatus) && @$opStatus['jit']['enabled'] ? 'enabled' : 'disabled/unavailable');
$p('PCRE JIT', ini_get('pcre.jit') ? 'enabled' : 'disabled');
$p('XDebug extension', extension_loaded('xdebug') ? 'enabled' : 'disabled');
$p('Benchmark Multiplier', $multiplier);
$p('Started at', DateTime::createFromFormat('U.u', microtime(true))->format('d/m/Y H:i:s.v'));
$p('', '', '-');

Expand Down

0 comments on commit 2ed951a

Please sign in to comment.