Commit a09cc6e
authored
standard: Optimize str_split() (php#20419)
Three optimizations:
- If the entire string is returned, we don't need to duplicate it.
- Use packed filling logic.
- Use fast construction of strings. This is useful when splitting
strings on length=1. In that case I get a 6x speedup in the code
below.
Bench:
```php
$x = str_repeat('A', 100);
for ($i = 0; $i < 1000000; $i++)
str_split($x, 10);
```
On an i7-4790:
```
Benchmark 1: ./sapi/cli/php x.php
Time (mean ± σ): 160.1 ms ± 6.4 ms [User: 157.3 ms, System: 1.8 ms]
Range (min … max): 155.6 ms … 184.7 ms 18 runs
Benchmark 2: ./sapi/cli/php_old x.php
Time (mean ± σ): 202.6 ms ± 4.0 ms [User: 199.1 ms, System: 1.9 ms]
Range (min … max): 197.4 ms … 209.2 ms 14 runs
Summary
./sapi/cli/php x.php ran
1.27 ± 0.06 times faster than ./sapi/cli/php_old x.php
```
The performance gain increases with smaller lengths.1 parent 376fdd7 commit a09cc6e
2 files changed
Lines changed: 17 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
| 131 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6152 | 6152 | | |
6153 | 6153 | | |
6154 | 6154 | | |
6155 | | - | |
| 6155 | + | |
| 6156 | + | |
6156 | 6157 | | |
6157 | 6158 | | |
6158 | 6159 | | |
6159 | 6160 | | |
| 6161 | + | |
6160 | 6162 | | |
6161 | 6163 | | |
6162 | 6164 | | |
6163 | 6165 | | |
6164 | | - | |
6165 | | - | |
6166 | | - | |
6167 | | - | |
| 6166 | + | |
| 6167 | + | |
| 6168 | + | |
| 6169 | + | |
| 6170 | + | |
| 6171 | + | |
| 6172 | + | |
| 6173 | + | |
6168 | 6174 | | |
6169 | | - | |
6170 | | - | |
6171 | | - | |
| 6175 | + | |
| 6176 | + | |
| 6177 | + | |
| 6178 | + | |
| 6179 | + | |
6172 | 6180 | | |
6173 | 6181 | | |
6174 | 6182 | | |
| |||
0 commit comments