Skip to content
Advertisement

How to improve the speed of a for loop in PHP?

I have a CSV file with different lines:

JavaScript

And in a folder of photos that must have the naming format “A1101_0090-1.JPG” for the first view for example.

I wrote a code that allows you to have two things:

  • the names of the images present in both the csv file and the photos folder, and the number of views
  • The names of images that are in the photos folder but not in the csv file or that are incorrectly renamed.

My script works but when I put a big photo folder with more than 5000 photos for example, the processing is very long… how could I improve my code ?

JavaScript

Advertisement

Answer

Don’t call glob(). Just use a loop that processes each file that matches the pattern in numeric order. You can stop the loop when the file doesn’t exist.

I assume there are no gaps in your numeric sequence of filenames.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement