Loops optimization #1155

Open
opened 2026-10-05 00:33:29 +02:00 by adam · 2 comments
Owner

For example:
for ($i=0; $i<count($page['chronology_date']); $i++)

This means on each loop, we call and recount count($page['chronology_date']).

For performance reasons we could do this:

for ($i=0, $pagecount = count($page['chronology_date']); $i<$pagecount; $i++)

For example: `for ($i=0; $i<count($page['chronology_date']); $i++)` This means on each loop, we call and recount count($page['chronology_date']). For performance reasons we could do this: `for ($i=0, $pagecount = count($page['chronology_date']); $i<$pagecount; $i++)` <!-- github-sync-id:802756716 -->
Author
Owner

Is the impact real? I mean if it's to reduce page generation time from 10ms to 9.94ms, it's certainly not where our energy should be focused.

Is the impact real? I mean if it's to reduce page generation time from 10ms to 9.94ms, it's certainly not where our energy should be focused. <!-- github-comment-id:774644638 -->
Author
Owner

It depends on the array length and how many loops we change.
The gain is more than linear, I mean, the more length has the array we are counting, the more performance we gain.

Defining the variable as I suggest is just following best practices without changing any real logic.

In the search file we changed 13 loops, that is one place where we could feel it.

It depends on the array length and how many loops we change. The gain is more than linear, I mean, the more length has the array we are counting, the more performance we gain. Defining the variable as I suggest is just following best practices without changing any real logic. In the search file we changed 13 loops, that is one place where we could feel it. <!-- github-comment-id:774722780 -->
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: adam/Piwigo#1155