New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
array_merge() being used on looping => docs #138
Comments
Itshould be like this (saves looooots of memory allocation rounds, what leads to better performace as well) $dupesIds = [];
foreach ($users as $user) {
$dupesIds []= $user->some_ids_array;
}
$dupesIds = call_user_func_array('array_merge', $dupesIds) |
I do some tests, and the problem is not the memory (in this case, inside looping is better), but the CPU usage (it's a looooooooot worse). Can you put your example on plugin (description)? It can help to users to follow it. Currently description is very unclear about other options. 😄
Some benchmarking (7.000 iterations): Case 1: with
Case 2: with
Extra case: with
|
You also can try prefixing call_user_func_array with root NS: '\call_user_func_array(...)' ;) |
It can gain some extra execution time benefits in PHP7+ |
No changes. I guess that it's a micro-micro optimization haha. Running on PHP 7.1. \count(\call_user_func_array('array_merge', $idsArray));
|
Okay, I think opcache is not used during the benchmarking. In the case opcache does pretty neat optimizations ) |
Documented here, feedback is welcome. |
Sorry for posting in a closed issue but I think it's worth mentioning that PHP will throw a Fatal error (
something like if (!empty($options) {
$options = array_merge(...$options);
} could be inserted |
No problems, it's pretty handy actually as the context already here - I'll update the docs. $options = [[]]; // the inner array prevents errors when invoking the array_merge after loop
... |
@rieschl : I updated the documentation, thanks for reporting =) |
[+]: optimize performance from "array_merge()" -> kalessil/phpinspectionsea#138
How about rewriting something like this without array_merge? foreach ($results as $key => $value) {
$results[$key]['breakdown'] = \array_merge($results[$key]['breakdown'], $extraAccessorials);
} Basically, merging $extraAccessorials to all elements of $results |
@akrz depending of your PHP version (requires 5.6+), you could do something like that: $breakdownRaw = [];
foreach ($results as $key => $value) {
// $extraAccessorials should be assigned at some point before.
$breakdownRaw[] = $extraAccessorials;
}
$results[$key]['breakdown'] = \array_merge($results[$key]['breakdown'], ... $breakdownRaw); Example: https://ideone.com/rmZ5jW (will reproduces |
@rentalhost Thanks for responding. Example of what I want: https://ideone.com/jgWMWo With your suggestion, $extraAccessorials will end up in the last element of $results: |
Oh! Now I understand your case. So I think that it should be just ignored by |
Spread comes to save the world. |
EDIT: needs to be documented here
I'm getting the reporting about using of
array_merge()
in loopings. My question is: what is the best way to not do that?For instance: the code below stores some data from
$user
, that is an array of int.The text was updated successfully, but these errors were encountered: