<?php class Benchmark { private $startTime; private $endTime; public function start() { $this->startTime = hrtime(true); } public function end() { $this->endTime = hrtime(true); } public function getElapsedTime() { return round(($this->endTime - $this->startTime) / 1_000_000_000, 8); } } // Example usage $benchmark = new Benchmark(); // ==================================== // PREPARE TEST DATA class A { private array $strings = []; public function prettifyTestMethodName1(string $name): string { if ($name === '') { return ''; } $string = rtrim($name, '0123456789'); if (array_key_exists($string, $this->strings)) { $name = $string; } elseif ($string === $name) { $this->strings[$string] = 1; } if (str_starts_with($name, 'test_')) { $name = substr($name, 5); } elseif (str_starts_with($name, 'test')) { $name = substr($name, 4); } if ($name === '') { return ''; } $name[0] = strtoupper($name[0]); $noUnderscore = str_replace('_', ' ', $name); if ($noUnderscore !== $name) { return trim($noUnderscore); } $wasNumeric = false; $buffer = ''; $len = strlen($name); for ($i = 0; $i < $len; $i++) { if ($i > 0 && $name[$i] >= 'A' && $name[$i] <= 'Z') { $buffer .= ' ' . strtolower($name[$i]); } else { $isNumeric = $name[$i] >= '0' && $name[$i] <= '9'; if (!$wasNumeric && $isNumeric) { $buffer .= ' '; $wasNumeric = true; } if ($wasNumeric && !$isNumeric) { $wasNumeric = false; } $buffer .= $name[$i]; } } return trim($buffer); } public function prettifyTestMethodName2(string $name): string { if ($name === '') { return ''; } $string = rtrim($name, '0123456789'); if (array_key_exists($string, $this->strings)) { $name = $string; } elseif ($string === $name) { $this->strings[$string] = 1; } if (str_starts_with($name, 'test_')) { $name = substr($name, 5); } elseif (str_starts_with($name, 'test')) { $name = substr($name, 4); } if ($name === '') { return ''; } $name = ucfirst($name); $noUnderscore = str_replace('_', ' ', $name); if ($noUnderscore !== $name) { return trim($noUnderscore); } $buffer = preg_replace_callback_array( [ '/(?!^)([A-Z])/' => static fn (array $matches) => ' ' . strtolower($matches[1]), '/(\d+)/' => static fn (array $matches) => ' ' . $matches[1], ], $name, ); return trim($buffer); } } class TestAbcdefghijabcdefGhijabcdefghijabcdefGhijabcdefghijabcdefghija23423bcdefghijabc2defghijabcdefghijabcdefghij { } // ==================================== $memory = memory_get_usage(); echo "Initial memory usage: " . ($memory / 1024) . " KB\n"; $benchmark->start(); // Code to benchmark for ($i = 0; $i < 100000; $i++) { $test1 = (new A())->prettifyTestMethodName1(TestAbcdefghijabcdefGhijabcdefghijabcdefGhijabcdefghijabcdefghija23423bcdefghijabc2defghijabcdefghijabcdefghij::class); } $benchmark->end(); $time1 = $benchmark->getElapsedTime(); echo "Elapsed time: " . $time1 . " seconds\n"; $memory = memory_get_usage(); echo "Memory usage after test 2: " . ($memory / 1024) . " KB\n"; echo "-----------------------------------\n"; $memory = memory_get_usage(); echo "Initial memory usage: " . ($memory / 1024) . " KB\n"; $benchmark->start(); // Code to benchmark for ($i = 0; $i < 100000; $i++) { $test2 = (new A())->prettifyTestMethodName2(TestAbcdefghijabcdefGhijabcdefghijabcdefGhijabcdefghijabcdefghija23423bcdefghijabc2defghijabcdefghijabcdefghij::class); } $benchmark->end(); $time2 = $benchmark->getElapsedTime(); echo "Elapsed time: " . $time2 . " seconds\n"; $memory = memory_get_usage(); echo "Memory usage after test 2: " . ($memory / 1024) . " KB\n"; echo "Times faster: " . round($time1 / $time2, 2) . "x\n"; assert($test1 === $test2, 'The two methods should produce the same result.' . var_export($test1, true) . ' !== ' . var_export($test2, true));
You have javascript disabled. You will not be able to edit any code.