<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Kommentare zu: PHP: foreach vs. while vs. for - The loop battle!</title>
	<atom:link href="http://juliusbeckmann.de/blog/php-foreach-vs-while-vs-for-the-loop-battle.html/feed" rel="self" type="application/rss+xml" />
	<link>http://juliusbeckmann.de/blog/php-foreach-vs-while-vs-for-the-loop-battle.html</link>
	<description>Ich bin nicht verrückt, nur technisch begabt ...</description>
	<lastBuildDate>Fri, 26 Sep 2014 12:04:55 -0400</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>Von: Richard Wardt van Duijvenbode</title>
		<link>http://juliusbeckmann.de/blog/php-foreach-vs-while-vs-for-the-loop-battle.html/comment-page-1#comment-1550</link>
		<dc:creator>Richard Wardt van Duijvenbode</dc:creator>
		<pubDate>Thu, 28 Feb 2013 02:03:36 +0000</pubDate>
		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=383#comment-1550</guid>
		<description>I did a simple benchmark too. You can find it over here: http://www.wardt.info/php-benchmark-the-speed-of-foreach-for-and-while-loops-compared/</description>
		<content:encoded><![CDATA[<p>I did a simple benchmark too. You can find it over here: <a href="http://www.wardt.info/php-benchmark-the-speed-of-foreach-for-and-while-loops-compared/" rel="nofollow">http://www.wardt.info/php-benchmark-the-speed-of-foreach-for-and-while-loops-compared/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: BergBenji</title>
		<link>http://juliusbeckmann.de/blog/php-foreach-vs-while-vs-for-the-loop-battle.html/comment-page-1#comment-1512</link>
		<dc:creator>BergBenji</dc:creator>
		<pubDate>Thu, 26 Jul 2012 08:48:10 +0000</pubDate>
		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=383#comment-1512</guid>
		<description>HI!
Thanks for the nice compare!

On my local system i have the same results. 
But if i use the Loops out of a function the foreach needs much more time.

function foreachLoop($array) {
    // Foreach
    $time_start = microtime(true);
    foreach($array as $a) {
      $devnull = $a;
    }
    $time_end = microtime(true);   
    return $time_end-$time_start;
}

This are my result:
0.142 seconds - foreach()
0.407 seconds - foreach() in function
0.159 seconds - while()
0.157 seconds - while() in function
0.150 seconds - for()
0.153 seconds - for() in function

I think speed depends on usage. Inline is the foreach faster. But in functions it is much slower.

Benji</description>
		<content:encoded><![CDATA[<p>HI!<br />
Thanks for the nice compare!</p>
<p>On my local system i have the same results.<br />
But if i use the Loops out of a function the foreach needs much more time.</p>
<p>function foreachLoop($array) {<br />
    // Foreach<br />
    $time_start = microtime(true);<br />
    foreach($array as $a) {<br />
      $devnull = $a;<br />
    }<br />
    $time_end = microtime(true);<br />
    return $time_end-$time_start;<br />
}</p>
<p>This are my result:<br />
0.142 seconds - foreach()<br />
0.407 seconds - foreach() in function<br />
0.159 seconds - while()<br />
0.157 seconds - while() in function<br />
0.150 seconds - for()<br />
0.153 seconds - for() in function</p>
<p>I think speed depends on usage. Inline is the foreach faster. But in functions it is much slower.</p>
<p>Benji</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: huck</title>
		<link>http://juliusbeckmann.de/blog/php-foreach-vs-while-vs-for-the-loop-battle.html/comment-page-1#comment-1482</link>
		<dc:creator>huck</dc:creator>
		<pubDate>Mon, 27 Feb 2012 23:13:29 +0000</pubDate>
		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=383#comment-1482</guid>
		<description>@mike: on my system this

$length = count($arr);
for($x = 0; $x &lt; $length; $x++)
{
	echo $arr[$x] . &quot;&quot;;
}

is about four times faster than this

for($x = 0, $length = count($arr); $x &lt; $length; $x++)
{
	echo $arr[$x] . &quot;&quot;;
}</description>
		<content:encoded><![CDATA[<p>@mike: on my system this</p>
<p>$length = count($arr);<br />
for($x = 0; $x &lt; $length; $x++)<br />
{<br />
	echo $arr[$x] . &quot;";<br />
}</p>
<p>is about four times faster than this</p>
<p>for($x = 0, $length = count($arr); $x &lt; $length; $x++)<br />
{<br />
	echo $arr[$x] . &quot;";<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: Julius</title>
		<link>http://juliusbeckmann.de/blog/php-foreach-vs-while-vs-for-the-loop-battle.html/comment-page-1#comment-1420</link>
		<dc:creator>Julius</dc:creator>
		<pubDate>Wed, 10 Aug 2011 19:01:22 +0000</pubDate>
		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=383#comment-1420</guid>
		<description>Hi Steven,
i had a short look on that and wrote some code for testing:
https://gist.github.com/1137776

Result:
while does not use any new ram, but the foreach loop will assign one new variable with &quot;foreach($array =&gt; $entry) {...&quot; when $entry is a new variable.
That is happening because the $entry variable wil stay after the loop is finished and has the last assigned value from array. But that should not result in noticeable ram increase, because $entry should be just a internal reference, as long as you have not modified it.

Hope that helps.</description>
		<content:encoded><![CDATA[<p>Hi Steven,<br />
i had a short look on that and wrote some code for testing:<br />
<a href="https://gist.github.com/1137776" rel="nofollow">https://gist.github.com/1137776</a></p>
<p>Result:<br />
while does not use any new ram, but the foreach loop will assign one new variable with "foreach($array =&gt; $entry) {..." when $entry is a new variable.<br />
That is happening because the $entry variable wil stay after the loop is finished and has the last assigned value from array. But that should not result in noticeable ram increase, because $entry should be just a internal reference, as long as you have not modified it.</p>
<p>Hope that helps.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: Steven T</title>
		<link>http://juliusbeckmann.de/blog/php-foreach-vs-while-vs-for-the-loop-battle.html/comment-page-1#comment-1418</link>
		<dc:creator>Steven T</dc:creator>
		<pubDate>Tue, 09 Aug 2011 13:58:04 +0000</pubDate>
		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=383#comment-1418</guid>
		<description>hello, wouldn&#039;t argue that foreach is definitely faster in speed, but do you have any test done for memory usage between the few? have yet to do some real benchmarking, but at least from my application, foreach seems to be using significantly more memory than for loop. 

any thoughts?</description>
		<content:encoded><![CDATA[<p>hello, wouldn't argue that foreach is definitely faster in speed, but do you have any test done for memory usage between the few? have yet to do some real benchmarking, but at least from my application, foreach seems to be using significantly more memory than for loop. </p>
<p>any thoughts?</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: Sam</title>
		<link>http://juliusbeckmann.de/blog/php-foreach-vs-while-vs-for-the-loop-battle.html/comment-page-1#comment-667</link>
		<dc:creator>Sam</dc:creator>
		<pubDate>Mon, 22 Mar 2010 09:11:53 +0000</pubDate>
		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=383#comment-667</guid>
		<description>Try this

&lt;code&gt;
...

// Generating test array
$array = array();
$i = 0;
while($i &lt; $n) {
    $array[] = md5($i++);
}

...
&lt;/code&gt;

This should be a better representation of a real world scenario. More often than not an array element will contain a string or another array so by hashing the numbers each element becomes 32 chars long.

When I run this modified benchmark the while loop comes out on top, and the foreach loop is the slowest.</description>
		<content:encoded><![CDATA[<p>Try this</p>
<div class="codesnip-container" >...</p>
<p>// Generating test array<br />
$array = array();<br />
$i = 0;<br />
while($i &lt; $n) {<br />
    $array[] = md5($i++);<br />
}</p>
<p>...</p></div>
<p>This should be a better representation of a real world scenario. More often than not an array element will contain a string or another array so by hashing the numbers each element becomes 32 chars long.</p>
<p>When I run this modified benchmark the while loop comes out on top, and the foreach loop is the slowest.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: Datenbankeinträge selectieren und löschen - php.de</title>
		<link>http://juliusbeckmann.de/blog/php-foreach-vs-while-vs-for-the-loop-battle.html/comment-page-1#comment-545</link>
		<dc:creator>Datenbankeinträge selectieren und löschen - php.de</dc:creator>
		<pubDate>Thu, 10 Dec 2009 09:40:21 +0000</pubDate>
		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=383#comment-545</guid>
		<description>[...] es dann nicht verwenden? Foreach ist wenn man dem hier glauben darf sogar ein stück schneller   PHP - foreach vs. while vs. for - The loop battle! &#124; Julius Beckmann      Geändert von Creator (Gestern um 19:57 [...]</description>
		<content:encoded><![CDATA[<p>[...] es dann nicht verwenden? Foreach ist wenn man dem hier glauben darf sogar ein stück schneller   PHP - foreach vs. while vs. for - The loop battle! | Julius Beckmann      Geändert von Creator (Gestern um 19:57 [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: Mike</title>
		<link>http://juliusbeckmann.de/blog/php-foreach-vs-while-vs-for-the-loop-battle.html/comment-page-1#comment-486</link>
		<dc:creator>Mike</dc:creator>
		<pubDate>Mon, 05 Oct 2009 21:23:21 +0000</pubDate>
		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=383#comment-486</guid>
		<description>Im almost always reading documentation on php.net but when its something that seems to be obvious - Im not.

Over the years it turned out that its ALWAYS good to read the whole documentation, even about the basic things - because theres always a posibility youll find something you didnt know before.

Example? For me, things like passing the whole arrays instead of singular variable to the particular built-in function, like strtr() - http://pl.php.net/strtr

Now Im using this with arrays in my own function that creates friendly URLs.

Sorry for offtop :).</description>
		<content:encoded><![CDATA[<p>Im almost always reading documentation on php.net but when its something that seems to be obvious - Im not.</p>
<p>Over the years it turned out that its ALWAYS good to read the whole documentation, even about the basic things - because theres always a posibility youll find something you didnt know before.</p>
<p>Example? For me, things like passing the whole arrays instead of singular variable to the particular built-in function, like strtr() - <a href="http://pl.php.net/strtr" rel="nofollow">http://pl.php.net/strtr</a></p>
<p>Now Im using this with arrays in my own function that creates friendly URLs.</p>
<p>Sorry for offtop :).</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: Julius</title>
		<link>http://juliusbeckmann.de/blog/php-foreach-vs-while-vs-for-the-loop-battle.html/comment-page-1#comment-485</link>
		<dc:creator>Julius</dc:creator>
		<pubDate>Mon, 05 Oct 2009 07:35:05 +0000</pubDate>
		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=383#comment-485</guid>
		<description>I prefer fast and tiny code :D

All what we mentioned is available in the PHP docu:
http://de3.php.net/manual/en/control-structures.for.php
Maybe we should a little bit more to save some time :D</description>
		<content:encoded><![CDATA[<p>I prefer fast and tiny code :D</p>
<p>All what we mentioned is available in the PHP docu:<br />
<a href="http://de3.php.net/manual/en/control-structures.for.php" rel="nofollow">http://de3.php.net/manual/en/control-structures.for.php</a><br />
Maybe we should a little bit more to save some time :D</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: Mike</title>
		<link>http://juliusbeckmann.de/blog/php-foreach-vs-while-vs-for-the-loop-battle.html/comment-page-1#comment-484</link>
		<dc:creator>Mike</dc:creator>
		<pubDate>Sun, 04 Oct 2009 22:40:57 +0000</pubDate>
		<guid isPermaLink="false">http://juliusbeckmann.de/blog/?p=383#comment-484</guid>
		<description>My pleasure :).

The situation I jumped on this idea was quite odd.  I have read some fiery discussion on the web, the topic was something like:

- faster script vs. less coding

There was rather big flame about the loops, whether its better to call the count() within the loop on every iteration or to call it just once before the loop, but with the cost of more lines of code.

I thought: wait a minute, after we call for() we define $i, but its done just this once, then the loop doesnt go back to it - so why dont we define one more variable there?

Then I gave it a shoot and tested in different situations -  it worked wery well so I just started to use it and tell people about it (if they didnt know this yet).

Cheers!</description>
		<content:encoded><![CDATA[<p>My pleasure :).</p>
<p>The situation I jumped on this idea was quite odd.  I have read some fiery discussion on the web, the topic was something like:</p>
<p>- faster script vs. less coding</p>
<p>There was rather big flame about the loops, whether its better to call the count() within the loop on every iteration or to call it just once before the loop, but with the cost of more lines of code.</p>
<p>I thought: wait a minute, after we call for() we define $i, but its done just this once, then the loop doesnt go back to it - so why dont we define one more variable there?</p>
<p>Then I gave it a shoot and tested in different situations -  it worked wery well so I just started to use it and tell people about it (if they didnt know this yet).</p>
<p>Cheers!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
