Commit 0943b8b
authored
Improve performance of array_find() etc (#18157)
This avoids destruction logic for the common case, avoids some copy, and
adds an optimization hint.
For this script:
```php
$array = range(1, 10000);
$result = 0;
for ($i = 0; $i < 5000; $i++) {
$result += array_find($array, static function ($item) {
return $item === 5000;
});
}
var_dump($result);
```
On an intel i7 1185G7:
```
Benchmark 1: ./sapi/cli/php x.php
Time (mean ± σ): 543.7 ms ± 3.8 ms [User: 538.9 ms, System: 4.4 ms]
Range (min … max): 538.4 ms … 552.9 ms 10 runs
Benchmark 2: ./sapi/cli/php_old x.php
Time (mean ± σ): 583.0 ms ± 4.2 ms [User: 578.4 ms, System: 3.4 ms]
Range (min … max): 579.3 ms … 593.9 ms 10 runs
Summary
./sapi/cli/php x.php ran
1.07 ± 0.01 times faster than ./sapi/cli/php_old x.php
```
On an intel i7 4790:
```
Benchmark 1: ./sapi/cli/php x.php
Time (mean ± σ): 828.6 ms ± 4.8 ms [User: 824.4 ms, System: 1.6 ms]
Range (min … max): 822.8 ms … 839.0 ms 10 runs
Benchmark 2: ./sapi/cli/php_old x.php
Time (mean ± σ): 940.1 ms ± 26.4 ms [User: 934.4 ms, System: 2.5 ms]
Range (min … max): 918.0 ms … 981.1 ms 10 runs
Summary
./sapi/cli/php x.php ran
1.13 ± 0.03 times faster than ./sapi/cli/php_old x.php
```1 parent 370f242 commit 0943b8b
1 file changed
+27
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6509 | 6509 | | |
6510 | 6510 | | |
6511 | 6511 | | |
| 6512 | + | |
| 6513 | + | |
| 6514 | + | |
| 6515 | + | |
| 6516 | + | |
| 6517 | + | |
| 6518 | + | |
| 6519 | + | |
| 6520 | + | |
| 6521 | + | |
| 6522 | + | |
| 6523 | + | |
| 6524 | + | |
| 6525 | + | |
| 6526 | + | |
| 6527 | + | |
6512 | 6528 | | |
6513 | 6529 | | |
6514 | 6530 | | |
| |||
6571 | 6587 | | |
6572 | 6588 | | |
6573 | 6589 | | |
6574 | | - | |
6575 | | - | |
6576 | | - | |
| 6590 | + | |
6577 | 6591 | | |
6578 | 6592 | | |
6579 | 6593 | | |
| |||
6597 | 6611 | | |
6598 | 6612 | | |
6599 | 6613 | | |
6600 | | - | |
| 6614 | + | |
6601 | 6615 | | |
6602 | 6616 | | |
6603 | 6617 | | |
| |||
6620 | 6634 | | |
6621 | 6635 | | |
6622 | 6636 | | |
| 6637 | + | |
| 6638 | + | |
| 6639 | + | |
6623 | 6640 | | |
6624 | 6641 | | |
6625 | 6642 | | |
6626 | 6643 | | |
6627 | 6644 | | |
6628 | | - | |
| 6645 | + | |
6629 | 6646 | | |
6630 | 6647 | | |
6631 | 6648 | | |
6632 | 6649 | | |
6633 | 6650 | | |
6634 | 6651 | | |
6635 | | - | |
6636 | | - | |
6637 | | - | |
6638 | | - | |
6639 | | - | |
6640 | | - | |
6641 | | - | |
| 6652 | + | |
6642 | 6653 | | |
6643 | 6654 | | |
6644 | 6655 | | |
| |||
6667 | 6678 | | |
6668 | 6679 | | |
6669 | 6680 | | |
6670 | | - | |
| 6681 | + | |
6671 | 6682 | | |
6672 | 6683 | | |
6673 | 6684 | | |
| |||
6683 | 6694 | | |
6684 | 6695 | | |
6685 | 6696 | | |
6686 | | - | |
| 6697 | + | |
6687 | 6698 | | |
6688 | 6699 | | |
6689 | 6700 | | |
| |||
6699 | 6710 | | |
6700 | 6711 | | |
6701 | 6712 | | |
6702 | | - | |
| 6713 | + | |
6703 | 6714 | | |
6704 | 6715 | | |
6705 | 6716 | | |
| |||
6715 | 6726 | | |
6716 | 6727 | | |
6717 | 6728 | | |
6718 | | - | |
| 6729 | + | |
6719 | 6730 | | |
6720 | 6731 | | |
6721 | 6732 | | |
| |||
0 commit comments