diff --git a/README.md b/README.md index f9d9c2e..f504837 100644 --- a/README.md +++ b/README.md @@ -1,44 +1,44 @@ # PHP-Performance + PHP Performance Measuring -This Project is for some PHP Peformance measuring tests in different scopes. -100 % is the best. -If you have some intresting Perfomance measurings, please extend this project :-) +This project contains several PHP performance measuring tests in different scopes. +100.0% represents the best performance. -https://phpsandbox.io/n/performance-strrpos-vs-strpos-substr-compare-vs-posvs-preg-match-op6yr +## Prerequisites -PHP 8.0: -``` -100.0% 0.36413s strrpos() -110.9% 0.40368s strrpos(+pos) -116.3% 0.42361s [x]===':' -121.9% 0.44401s strpos() -144.6% 0.52642s substr_compare() -155.4% 0.56575s strpos(+pos) -256.2% 0.93287s preg_match -``` -https://phpsandbox.io/n/performancetest-isset-vs-vs-ignoringerror-a6gaa +- PHP >= 7.0 +- `bcmath` extension (used for precision in performance reporting) -PHP 8.0: -``` -100.0% 0.02643s isset() -112.4% 0.02969s ?? -944.9% 0.24970s No Check -``` +## Available Benchmarks -https://phpsandbox.io/n/differenceand-performance-between-str-replace-vs-strtr-mmrck -PHP 8.0: -``` -str_replace: -ZbcZZZZbcZZZZbcZZZZbcZZZZbcZZZZbcZZZZbcZZZZbcZZZZbcZZZZbcZZZ -strtr: -ZbcyzaZbcyzaZbcyzaZbcyzaZbcyzaZbcyzaZbcyzaZbcyzaZbcyzaZbcyza -100.0% 0.02024s strtr() -179.4% 0.03631s str_replace() -str_replace: -ZbcaaaZbcaaaZbcaaaZbcaaaZbcaaaZbcaaaZbcaaaZbcaaaZbcaaaZbcaaa -strtr: -ZbcyzaZbcyzaZbcyzaZbcyzaZbcyzaZbcyzaZbcyzaZbcyzaZbcyzaZbcyza -100.0% 0.02041s strtr() -138.8% 0.02833s str_replace() +### 1. Timestamp Detection (`is_timestamp.php`) +Compares different ways to find a colon in a string (simulating part of a timestamp validation). +- `strrpos()` +- `strpos()` +- `substr_compare()` +- Direct index access `[x]===':'` +- `preg_match` + +### 2. Property/Array Access Checks (`property_is_set.php`) +Compares different ways to check if an array key is set. +- `isset()` +- Null coalescing operator `??` +- No check (ignoring errors) + +### 3. String Replacement (`str_replace_vs_strtr.php`) +Compares performance and behavior differences between `str_replace()` and `strtr()`. + +## How to Run + +You can run any benchmark directly using the PHP CLI: + +```bash +php is_timestamp.php +php property_is_set.php +php str_replace_vs_strtr.php ``` + +## Contributing + +If you have interesting performance measurements, please feel free to extend this project! diff --git a/composer.json b/composer.json index ba94334..fb1f0c4 100644 --- a/composer.json +++ b/composer.json @@ -2,6 +2,6 @@ "name": "fglueck/perf", "description": "Performance measuring PHP Class", "require": { - "php": ">=7.0", + "php": ">=7.0" } } diff --git a/is_timestamp.php b/is_timestamp.php index a782a1a..df6f0a1 100644 --- a/is_timestamp.php +++ b/is_timestamp.php @@ -1,3 +1,4 @@ +test('strrpos()', function () use ($string) { - if(strrpos($string, ':')); + if(strrpos($string, ':') !== false); }); $p->test('strrpos(+pos)', function () use ($string) { - if(strrpos($string, ':', -4)); + if(strrpos($string, ':', -4) !== false); }); $p->test('strpos()', function () use ($string) { - if(strpos($string, ':')); + if(strpos($string, ':') !== false); }); $p->test('strpos(+pos)', function () use ($string) { - if(strpos($string, ':', 12)); + if(strpos($string, ':', 12) !== false); }); $p->test('substr_compare()', function () use ($string) { - if(substr_compare($string, ':', -3,1)===1); + if(substr_compare($string, ':', -3,1)===0); }); $len = strlen($string); $p->test("[x]===':'", function () use ($string, $len) { - if($len-3>0 and $string[$len-3]===':'); + if($len-3>=0 and $string[$len-3]===':'); }); $p->test('preg_match', function () use ($string) { diff --git a/poperty_is_set.php b/property_is_set.php similarity index 97% rename from poperty_is_set.php rename to property_is_set.php index 8598627..8629e3a 100644 --- a/poperty_is_set.php +++ b/property_is_set.php @@ -39,7 +39,7 @@ public function report() { $p->start(); for($i=1000000;$i;$i--) { - if($x->y['']??''!==''); + if(($x->y['']??'') !==''); } $p->stop('??'); $p->report();