Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 36 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -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!
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "fglueck/perf",
"description": "Performance measuring PHP Class",
"require": {
"php": ">=7.0",
"php": ">=7.0"
}
}
13 changes: 7 additions & 6 deletions is_timestamp.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?php
# https://phpsandbox.io/n/performance-strrpos-vs-strpos-substr-compare-vs-posvs-preg-match-op6yr
#error_reporting (0);

Expand Down Expand Up @@ -45,28 +46,28 @@ public function test(string $label, callable $call) {
$p = new perf(1000000);

$p->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) {
Expand Down
2 changes: 1 addition & 1 deletion poperty_is_set.php → property_is_set.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function report() {

$p->start();
for($i=1000000;$i;$i--) {
if($x->y['']??''!=='');
if(($x->y['']??'') !=='');
}
$p->stop('??');
$p->report();