Method two is unmaintainable. If the size of $data changes, you’d have to add or delete code lines commensurate, and your script wouldnt work correctly until you do.
(though you seem to have confused yourself with a for loop?)
Also I sincerely hope you’re not sending 100,000 individual emails on a regular basis?
I’d expect that the second code example wouldn’t have each of the 1000 email calls hand-coded like that, it would be another for loop to work through each $chunk array, and wouldn’t it run 0-999 rather than 0-1000? It would need to run to count($chunk[n]) in case the main array doesn’t happen to have exactly the expected number of elements.
I’d expect that the biggest thing affecting how much time either of these take is the sending of the emails. If you want to see which actual looping method is faster, comment out the code that sends the emails and just run the loops, and time them. I wouldn’t be surprised if each attempt to send an email takes a different amount of time, just because you’re adding things in which are out of your control, such as how quickly the SMTP server responds.
You could probably make the first one very slightly quicker by changing this
$email = $row['email'];
send_email($email);
to this
send_email($row['email']);
and not creating a variable for no apparent reason.
two for loops vs two foreaches is… almost entirely semantics?
This bunch of stuff:
doesnt do anything.
$chunkTotal is only used in the for loop initializer and can be substituted.
$i is defined by the loop and is moot.
$stopped is a value to be substituted,
$resume is never used.
As far as I can tell all array_chunk does here is duplicate the array into several more arrays which you then loop one by one. So its uses more memory, but isn’t actually made any faster. If anything, using array_chunk makes it slower.
When you define a variable, PHP has to put it into its variable table. That takes up space, and time to do it. It’s small - microscopically so, but it happens.
I’m guessing most recommendations are suggesting chunking it into smaller parts and operating on one chunk per execution.
The two blocks of code execute 1 million (strictly, they execute more like 3.01 million, but…) operations, regardless. Execution time is based on the script as a whole, not the block.
So you need to test your code, benchmark how long it takes to run the operations you want it to run on a single value set, and extrapolate your need from there.
If the function takes 29 seconds to operate on a SINGLE record, chunking in chunks of 1000 wont work either.