DEV Community

Cover image for php: Optimise forloop with this tip
Chidiebere Chukwudi
Chidiebere Chukwudi

Posted on • Edited on • Originally published at jovialcore.tech

2

php: Optimise forloop with this tip

This is not a long lists of whats and what-not-to-do when using the forloop syntax in php. It's just a tip I got from official php doc. Also this tip is not specific to php

For the following example, take note of the count() method:

Sometimes, It's a common thing to many php developers to iterate through arrays like in the example below.

<?php
$vehicles = array(
    array('name' => 'toyota', 'salt' => 856412),
    array('name' => 'ford', 'salt' => 215863)
);

for($i = 0; $i < count($vehicles); ++$i) {
    $people[$i]['salt'] = mt_rand(000000, 999999);
}
?>
Enter fullscreen mode Exit fullscreen mode

According to official php doc, the above code can be slow because it has to count count($people); the array size for every iteration but most of the time, the size is usually constant hence, you can optimise by using an intermediate variable to hold/store the size instead of repeatedly calling count().. See example below:

<?php
$vehicles = array(
    array('name' => 'toyota', 'salt' => 856412),
    array('name' => 'ford', 'salt' => 215863)
);
$size = count($vehicles);
for($i = 0; $i < $size; ++$i) {
    $people[$i]['salt'] = mt_rand(000000, 999999);
}
?>
Enter fullscreen mode Exit fullscreen mode

That's it!

Let me know what you think?
Cover Image credits

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (4)

Collapse
 
moopet profile image
Ben Sinclair

Unless it's a very big array, this won't make much difference. Still, I'd use foreach instead, because you don't need to keep track of the index then. Better yet, for something like this, I'd use array_map.

Collapse
 
darkain profile image
Vincent Milum Jr

The array size doesn't actually matter for count(). The size of the array is stored along with the array contents, so its not actually "counting" the number of objects each time, instead just pulling that value. PHP's documentation is a simplistic example, but in reality, it is only saving a couple CPU cycles at best. It is a very VERY minor "micro-optimization" when it comes to count(), but would be more impactful for a more computationally complex method call.

Collapse
 
joolsmcfly profile image
Julien Dephix

Note that this is not specific to php.

Collapse
 
jovialcore profile image
Chidiebere Chukwudi

Oh...I did not really think in that direction, may be I should just update the post. Thanks for your contribution.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay