[php-src] PR #13558: Optimize strpbrk()
| From: | nielsdos | Date: | Thu, 29 Feb 2024 16:46:19 +0000 |
| Subject: | [php-src] PR #13558: Optimize strpbrk() | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-25201@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/13558
Author: nielsdos
strpbrk() can be implemented using strcspn(). As we have an optimized strcspn() implementation, we
can do better than a nested for-loop. This is especially beneficial when the second argument
contains more than one character.
When testing the following PHP code, I compared the run time in ms:
```php
$in = str_repeat('a', (int)$argv[1]) . ' ';
for ($i = 0; $i < 3000**2; $i++) {
strpbrk($in, ' ');
}
```

Similarly, I have a test variant with multiple characters in the second argument:
```php
$in = str_repeat('a', (int)$argv[1]) . ' ';
for ($i = 0; $i < 3000**2; $i++) {
strpbrk($in, 'qwerty ');
}
```
