This is a quick code profiler/benchmarker for PHP functions. Instantiate the class, load it with the tests you require, then let it run.
<?php
$runner = new Runner();
// NOTE: Test parameters are passed with an array, as per call_user_func_array.
$runner->setTestParameters([eval(str_replace('#', 'null', 'return [[#,#,#],[#' .
'],[],#,[["name"=>"PHP_VERSION","value"=>"8.3.7"],["name"=>"PHP_MAJOR_VERSION' .
'","value"=>8],["name"=>"PHP_MINOR_VERSION","value"=>3],["name"=>"PHP_RELEASE' .
'_VERSION","value"=>7],["name"=>"PHP_EXTRA_VERSION","value"=>""],["name"=>"PH' .
'P_VERSION_ID","value"=>80307],["name"=>"PHP_ZTS","value"=>0]],["name"=>"PHP_' .
'SAPI","value"=>""],[#],["name"=>"PHP_BINARY","value"=>""],[],#,[],[],[],[#,#' .
',#,#,#,#,#,#],[],[#],[#,#,#,#,#,#,#],[],[#,#],#,#,#,#,[#],#,#];'
))]);
$runner->addTestCase('foreach-loop', function ($constants) {
foreach ($constants as $index => &$constant) {
if (is_null($constant)) {
unset($constants[$index]);
continue;
}
// If this is a constant registration value, skip it.
if (array_key_exists('name', $constant)) {
continue;
}
// Removes NULL values from the second level of the array.
$constants[$index] = array_filter($constants[$index]);
// If we now have an empty array, we should remove it altogether.
if (empty($constants[$index])) {
unset($constants[$index]);
continue;
}
}
});
$runner->addTestCase('functional', function ($constants) {
// Removes NULL values from the first level of the array.
$constants = array_filter($constants);
// Removes all NULL values from the second level of the array.
$constants = array_map('array_filter', $constants);
// Removes all empty second level arrays.
$constants = array_filter($constants);
});
$runner->addTestCase('example-really-quick', function ($constants) {
// Do nothing
});
$runner->run();
php test.php
Beginning tests. Tests may run up to 10 seconds.
+----------------+--------------+------------+----------------------+
| | foreach-loop | functional | example-really-quick |
+----------------+--------------+------------+----------------------+
| numTests | 389 | 389 | 389 |
| averageMemory | 1.2KB | 3.57KB | 256B |
| totalMemory | 468.23KB | 1.36MB | 97.25KB |
| averageRuntime | 21.55ms | 4.29ms | 941.26us |
| totalRuntime | 8.38s | 1.67s | 366.15ms |
+----------------+--------------+------------+----------------------+
Total test time: 10.42s