[php-src] Issue #13710: `rtrim` equal fast to `preg_replace`
| From: | staabm | Date: | Thu, 14 Mar 2024 14:35:26 +0000 |
| Subject: | [php-src] Issue #13710: `rtrim` equal fast to `preg_replace` | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-246580@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/13710
Author: staabm
### Description
I am currently doing some micro benchmarks and tried several things to speedup phpunit in some form.
one idea I had when looking at a hotpath of a app was to replace
```
$string = (string) preg_replace('#\d+$#', '', $name, -1, $count);
```
with
```
$string = rtrim($name, '0123456789')
```
see
```
mstaab@mst22:/www/www$ hyperfine --warmup 10 'php preg.php' 'php rtrim.php'
Benchmark 1: php preg.php
Time (mean ± σ): 56.9 ms ± 9.0 ms [User: 30.6 ms, System: 25.7 ms]
Range (min … max): 43.8 ms … 80.4 ms 41 runs
Benchmark 2: php rtrim.php
Time (mean ± σ): 58.7 ms ± 9.2 ms [User: 34.0 ms, System: 24.0 ms]
Range (min … max): 43.8 ms … 83.0 ms 46 runs
Summary
'php preg.php' ran
1.03 ± 0.23 times faster than 'php rtrim.php'
mstaab@mst22:/www/www$ php -v
PHP 8.1.27 (cli) (built: Dec 21 2023 20:19:54) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.27, Copyright (c) Zend Technologies
```
### PHP Version
8.1.27
### Operating System
ubuntu22.04 lts