3 min read

The problem with for loops

The problem with for loops

Everybody should know a simple programming concept called a “for loop“.

I think of it as a tool that enables me to do some things repeatedly.  For instance, the above example prints the numbers from 0 to 99.

One clear benefit of using a for loop is that you don’t need to write the same code multiple times to reuse it. You can just repeat and cycle through it.

I love this tool as it’s at the basis of automation and I use a variation of it, the foreach loop, extensively.

Not anymore.

I’m always thinking about what I can cut from my code that will make me faster, and also maybe the application faster.

Lately, I’ve been switching my mindset from “repeating stuff” to “running stuff concurrently”. 

In that sense, every for or foreach loop is an opportunity for me to make things run at the same time, instead of one after another.

Computing power is cheaper than ever nowadays, there’s no sense in waiting for the first iteration to complete. You can just run multiple iterations simultaneously. 

PHP (which is the programming language I mostly use these days) is not a great language to do this imo, but here are a few applications of this concept.

#1 Batch cURL requests

Express, n00b explanation: cURL is a URL client. Imagine you wanted to download the source code of two sites: example.com and php.net – you can do that with cURL.

Instead of doing a single cURL request to download the first website’ source code, and then do the request to download the other one’s – you can just use a thing called curl_multi_exec
This code will download both sources at the same time! I use this extensively doing upwards of 20-30 concurrent requests at a time.

It uses more bandwidth, but you save a lot of time (especially if you need to request something in real-time on the front-end).

#2 Scale cron jobs

Express, n00b explanation: a cron job is a time-based job. You can tell your computer to ping Google every minute. That’s a cron job. 

I use cron job extensively to get data from different sources. At any time, I have 100s (sometimes 1000s) of these jobs running on my servers.

Now, the problem is that you can, at a maximum, run a cronjob every minute. What if I wanted to go faster? What if I wanted to ping Google every second?

Well, I can combine code and cron jobs to achieve that.

I’ve been using this library pretty often with the intent of calling a function multiple times per minute, and it works!

Obviously, I’m not entirely quitting using for loops, but I’m exploring how to make more and more jobs running at the same time. 

So now, it’s your turn: what will you make concurrent?

Hope this helps!

Enjoyed this article?

Join my newsletter to learn when new products are launched, as well as other stories from the trenches. People from great companies like Facebook and Hubspot read my newsletter.