Aller au contenu

Repeated work in loop (ARCH7072)

Ce contenu n’est pas encore disponible dans votre langue.

Key php.performance.repeated-work-in-loop · Severity info · Category performance

A computation whose inputs do not depend on the loop variable is re-run on every iteration. Hoist this loop-invariant expression out of the loop.

<?php
foreach ($items as $item) {
$limit = $config->get('max') * count($defaults);
process($item, $limit);
}
<?php
$limit = $config->get('max') * count($defaults);
foreach ($items as $item) {
process($item, $limit);
}

Move computations that do not depend on the loop variable above the loop so they run once.