Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

php - Exploding an array within a foreach loop parameter

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I could make an educated guess, but let's try it out!

I figured there were three main ways to approach this.

  1. explode and assign before entering the loop
  2. explode within the loop, no assignment
  3. string tokenize

My hypotheses:

  1. probably consume more memory due to assignment
  2. probably identical to #1 or #3, not sure which
  3. probably both quicker and much smaller memory footprint

Approach

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
", memory_get_peak_usage()/1024);
printf("Total time: %s s
", $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(',')) {}

Results

explode() benchmark results

Conclusions

Looks like some assumptions were disproven. Don't you love science? :-)

  • In the big picture, any of these methods is sufficiently fast for a list of "reasonable size" (few hundred or few thousand).
  • If you're iterating over something huge, time difference is relatively minor but memory usage could be different by an order of magnitude!
  • When you explode() inline without pre-assignment, it's a fair bit slower for some reason.
  • Surprisingly, tokenizing is a bit slower than explicitly iterating a declared array. Working on such a small scale, I believe that's due to the call stack overhead of making a function call to 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.

Final thought

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...