-
Notifications
You must be signed in to change notification settings - Fork 7.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Is it possible to make first class callables faster? #16759
Comments
Part of the reason of this is definitely the fact you're recreating a callable object over and over in the loop. If you bring |
Oh yeah, I didn't think about that. It helped a bit but it is still a lot slower. <?php
$a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
$s = microtime(true);
$f = strtoupper(...);
for($i = 0; $i < 100_000; $i++) {
$u = array_map($f, $a);
}
echo (microtime(true) - $s) . PHP_EOL;
$s = microtime(true);
for($i = 0; $i < 100_000; $i++) {
$u = array_map('strtoupper', $a);
}
echo (microtime(true) - $s) . PHP_EOL;
$s = microtime(true);
$f = fn($v) => strtoupper($v);
for($i = 0; $i < 100_000; $i++) {
$u = array_map($f, $a);
}
echo (microtime(true) - $s) . PHP_EOL;
|
I get very different results on a release compile of 8.4-dev:
FWIW I tried this on 8.3 too:
So pretty much the same. |
I just updated to PHP 8.3.13 locally and yes it is much closer now. I guess I'll close the issue :) Defined outside of the loop:
Defined in the loop:
|
Description
First class callables feel like the right way to do callbacks since you get propper completion from from your IDE/editor but the performance hit is rather bad. I'm sadly not very familiar with C or PHP internals but would it be possible for opcache to get performance parity with the "string" version of the callbacks? Maybe not for userland functions but at least for internal functions.
3v4l.org with PHP 8.3.13
3v4l.org with git.master
The text was updated successfully, but these errors were encountered: