foreach(explode(',' $foo) as $bar) { ... }
vs
$test = explode(',' $foo);
foreach($test as $bar) { ... }
In the first example, does it explode
the $foo
string for each iteration or does PHP keep it in memory exploded in its own temporary variable? From an efficiency point of view, does it make sense to create the extra variable $test
or are both pretty much equal?
php - Exploding an array within a foreach loop parameter - Stack Overflow foreach(explode(',' $foo) as $bar) { ... } vs $test = explode(',' $foo); foreach($test as $bar) { ... } In the first example, does it explode the $foo string for each iteration or does PHP keep ... Stack Overflow About Products For Teams
Firstly, to loop through an array by using the forEach method, you need a callback function (or anonymous function): The function will be executed for every single element of the array. It must take at least one parameter which represents the elements of an array:
C# language provides several techniques to read a collection of items. One of which is foreach loop. The foreach loop provides a simple, clean way to iterate through the elements of an collection or an array of items. One thing we must know that before using foreach loop we must declare the array or the collections in the program.
The forEach method passes a callback function for each element of an array together with the following parameters: 1 Current Value (required) - The value of the current array element 2 Index (optional) - The current element's index number 3 Array (optional) - The array object to which the current element belongs More ...
I could make an educated guess, but let's try it out!
I figured there were three main ways to approach this.
My hypotheses:
Here's my test script:
<?php
ini_set('memory_limit', '1024M');
$listStr = 'text';
$listStr .= str_repeat(',text', 9999999);
$timeStart = microtime(true);
/*****
* {INSERT LOOP HERE}
*/
$timeEnd = microtime(true);
$timeElapsed = $timeEnd - $timeStart;
printf("Memory used: %s kB\n", memory_get_peak_usage()/1024);
printf("Total time: %s s\n", $timeElapsed);
And here are the three versions:
1)
// explode separately
$arr = explode(',', $listStr);
foreach ($arr as $val) {}
2)
// explode inline-ly
foreach (explode(',', $listStr) as $val) {}
3)
// tokenize
$tok = strtok($listStr, ',');
while ($tok = strtok(',')) {}
Looks like some assumptions were disproven. Don't you love science? :-)
explode()
inline without pre-assignment, it's a fair bit slower for some reason.strtok()
every iteration. More on this below.In terms of number of function calls, explode()
ing really tops tokenizing. O(1) vs O(n)
I added a bonus to the chart where I run method 1) with a function call in the loop. I used strlen($val)
, thinking it would be a relatively similar execution time. That's subject to debate, but I was only trying to make a general point. (I only ran strlen($val)
and ignored its output. I did not assign it to anything, for an assignment would be an additional time-cost.)
// explode separately
$arr = explode(',', $listStr);
foreach ($arr as $val) {strlen($val);}
As you can see from the results table, it then becomes the slowest method of the three.
This is interesting to know, but my suggestion is to do whatever you feel is most readable/maintainable. Only if you're really dealing with a significantly large dataset should you be worried about these micro-optimizations.
In the first case, PHP explodes it once and keeps it in memory.
The impact of creating a different variable or the other way would be negligible. PHP Interpreter would need to maintain a pointer to a location of next item whether they are user defined or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With