<?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>Comments on: Improve performace: check your loops</title>
	<atom:link href="http://www.jasny.net/articles/improve-performace-check-your-loops/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jasny.net/articles/improve-performace-check-your-loops/</link>
	<description>It&#039;s all about me, mysql and Einstein.</description>
	<lastBuildDate>Wed, 08 Sep 2010 17:54:42 +0100</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Arnold Daniels</title>
		<link>http://www.jasny.net/articles/improve-performace-check-your-loops/comment-page-1/#comment-10197</link>
		<dc:creator>Arnold Daniels</dc:creator>
		<pubDate>Fri, 01 Feb 2008 11:09:00 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/improve-performace-check-your-loops/#comment-10197</guid>
		<description>Thanks Ian, I&#039;ve been lazy... :/ I should have tested the code.
The comma&#039;s however are correct. If you look closely you see that they are only within the string and relate to the echo in the line of code created for create_function.</description>
		<content:encoded><![CDATA[<p>Thanks Ian, I&#8217;ve been lazy&#8230; :/ I should have tested the code.<br />
The comma&#8217;s however are correct. If you look closely you see that they are only within the string and relate to the echo in the line of code created for create_function.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian</title>
		<link>http://www.jasny.net/articles/improve-performace-check-your-loops/comment-page-1/#comment-10150</link>
		<dc:creator>Ian</dc:creator>
		<pubDate>Fri, 01 Feb 2008 01:27:51 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/improve-performace-check-your-loops/#comment-10150</guid>
		<description>The code that you use demonstrate building an HTML table with create_function() has several parse errors.  The final line that is supposed to append to $code actually overwrites the entire variable, and I believe all of the commas you use to join strings within the $code string you are building should actually be the dot (concat) character.</description>
		<content:encoded><![CDATA[<p>The code that you use demonstrate building an HTML table with create_function() has several parse errors.  The final line that is supposed to append to $code actually overwrites the entire variable, and I believe all of the commas you use to join strings within the $code string you are building should actually be the dot (concat) character.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arnold Daniels</title>
		<link>http://www.jasny.net/articles/improve-performace-check-your-loops/comment-page-1/#comment-9902</link>
		<dc:creator>Arnold Daniels</dc:creator>
		<pubDate>Tue, 29 Jan 2008 11:20:08 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/improve-performace-check-your-loops/#comment-9902</guid>
		<description>For other readers: http://en.wikipedia.org/wiki/Duff%27s_device

If the processing time of the following code is 120ms.
&#160;&#160;for ($i=0; $i&lt;1000000; $i++) {
&#160;&#160;&#160;&#160;// Do nothing
&#160;&#160;}

And the processing time of this code is 80ms.
&#160;&#160;$i = 1000000;
&#160;&#160;$n = (int)($i % 8); // first do a modulo
&#160;&#160;$c = 0; // just to check the result
&#160;&#160;
&#160;&#160;if ($n &gt; 0) {
&#160;&#160;&#160;&#160;while ($n--) {
&#160;&#160;&#160;&#160;&#160;&#160;// Do nothing
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;}
&#160;&#160;}
&#160;&#160;
&#160;&#160;$n = (int)($i / 8);
&#160;&#160;
&#160;&#160;if ($n &gt; 0) {
&#160;&#160;&#160;&#160;while ($n--) {
&#160;&#160;&#160;&#160;&#160;&#160;// Do nothing
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;&#160;&#160;// Do nothing
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;&#160;&#160;// Do nothing
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;&#160;&#160;// Do nothing
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;&#160;&#160;// Do nothing
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;&#160;&#160;// Do nothing
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;&#160;&#160;// Do nothing
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;&#160;&#160;// Do nothing
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;}
&#160;&#160;}
You would indeed save a third of the processing time.

But that is only on the processing time for the loop itself. The processing time of the code in the loop doesn&#039;t change.

Lets say the following code would take about 1120ms to process (forgetting the overhead of calling time_nanosleep):
&#160;&#160;for ($i=0; $i&lt;1000000; $i++) {
&#160;&#160;&#160;&#160;time_nanosleep(0, 1000); //sleep for 1ms
&#160;&#160;}

If we would compare it to the processing time of this code:
&#160;&#160;$i = 1000000;
&#160;&#160;$n = (int)($i % 8); // first do a modulo
&#160;&#160;$c = 0; // just to check the result
&#160;&#160;
&#160;&#160;if ($n &gt; 0) {
&#160;&#160;&#160;&#160;while ($n--) {
&#160;&#160;&#160;&#160;&#160;&#160;time_nanosleep(0, 1000); //sleep 1 ms
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;}
&#160;&#160;}
&#160;&#160;
&#160;&#160;$n = (int)($i / 8);
&#160;&#160;
&#160;&#160;if ($n &gt; 0) {
&#160;&#160;&#160;&#160;while ($n--) {
&#160;&#160;&#160;&#160;&#160;&#160;time_nanosleep(0, 1000); //sleep 1 ms
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;&#160;&#160;time_nanosleep(0, 1000); //sleep 1 ms
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;&#160;&#160;time_nanosleep(0, 1000); //sleep 1 ms
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;&#160;&#160;time_nanosleep(0, 1000); //sleep 1 ms
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;&#160;&#160;time_nanosleep(0, 1000); //sleep 1 ms
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;&#160;&#160;time_nanosleep(0, 1000); //sleep 1 ms
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;&#160;&#160;time_nanosleep(0, 1000); //sleep 1 ms
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;&#160;&#160;time_nanosleep(0, 1000); //sleep 1 ms
&#160;&#160;&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;}
&#160;&#160;}
This function would take 1080ms to process and not 750ms, saving you only 3.5%.

This is part of the point I wanted to make. Don&#039;t take things out of context and go and benchmark them, because it won&#039;t help you in real-life situations. A Duff&#039;s device is a nice method of squeezing some extra performance out of code that is already optimized. But simply by using PHP that is hardly the case. If you&#039;re you want great performance you should just program that part in C, that will probably increase the performance by an order of magnitude.</description>
		<content:encoded><![CDATA[<p>For other readers: <a href="http://en.wikipedia.org/wiki/Duff%27s_device" rel="nofollow">http://en.wikipedia.org/wiki/Duff%27s_device</a></p>
<p>If the processing time of the following code is 120ms.<br />
&nbsp;&nbsp;for ($i=0; $i&lt;1000000; $i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;// Do nothing<br />
&nbsp;&nbsp;}</p>
<p>And the processing time of this code is 80ms.<br />
&nbsp;&nbsp;$i = 1000000;<br />
&nbsp;&nbsp;$n = (int)($i % 8); // first do a modulo<br />
&nbsp;&nbsp;$c = 0; // just to check the result<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;if ($n &gt; 0) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;while ($n&#8211;) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do nothing<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;$n = (int)($i / 8);<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;if ($n &gt; 0) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;while ($n&#8211;) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do nothing<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do nothing<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do nothing<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do nothing<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do nothing<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do nothing<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do nothing<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do nothing<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
You would indeed save a third of the processing time.</p>
<p>But that is only on the processing time for the loop itself. The processing time of the code in the loop doesn&#8217;t change.</p>
<p>Lets say the following code would take about 1120ms to process (forgetting the overhead of calling time_nanosleep):<br />
&nbsp;&nbsp;for ($i=0; $i&lt;1000000; $i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;time_nanosleep(0, 1000); //sleep for 1ms<br />
&nbsp;&nbsp;}</p>
<p>If we would compare it to the processing time of this code:<br />
&nbsp;&nbsp;$i = 1000000;<br />
&nbsp;&nbsp;$n = (int)($i % 8); // first do a modulo<br />
&nbsp;&nbsp;$c = 0; // just to check the result<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;if ($n &gt; 0) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;while ($n&#8211;) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time_nanosleep(0, 1000); //sleep 1 ms<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;$n = (int)($i / 8);<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;if ($n &gt; 0) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;while ($n&#8211;) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time_nanosleep(0, 1000); //sleep 1 ms<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time_nanosleep(0, 1000); //sleep 1 ms<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time_nanosleep(0, 1000); //sleep 1 ms<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time_nanosleep(0, 1000); //sleep 1 ms<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time_nanosleep(0, 1000); //sleep 1 ms<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time_nanosleep(0, 1000); //sleep 1 ms<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time_nanosleep(0, 1000); //sleep 1 ms<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time_nanosleep(0, 1000); //sleep 1 ms<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
This function would take 1080ms to process and not 750ms, saving you only 3.5%.</p>
<p>This is part of the point I wanted to make. Don&#8217;t take things out of context and go and benchmark them, because it won&#8217;t help you in real-life situations. A Duff&#8217;s device is a nice method of squeezing some extra performance out of code that is already optimized. But simply by using PHP that is hardly the case. If you&#8217;re you want great performance you should just program that part in C, that will probably increase the performance by an order of magnitude.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Unomi</title>
		<link>http://www.jasny.net/articles/improve-performace-check-your-loops/comment-page-1/#comment-9893</link>
		<dc:creator>Unomi</dc:creator>
		<pubDate>Tue, 29 Jan 2008 10:20:08 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/improve-performace-check-your-loops/#comment-9893</guid>
		<description>Arnold,

Sorry, I can&#039;t follow you.... How do you compare 40ms on seconds, while before you take 120ms and take a third (40ms)?

A third of a few seconds (say 6) is still (2) seconds gain.

Instead of:

for ($i = 1933999; $i &gt; 0; $i--) {
&#160;&#160;// do some stuff
}

You could use a Duff&#039;s device and save some seconds. I benchmarked it with real functions and gained a third in speed. 

So, if it doesn&#039;t seems worth the trouble... don&#039;t use it.

- Unomi -</description>
		<content:encoded><![CDATA[<p>Arnold,</p>
<p>Sorry, I can&#8217;t follow you&#8230;. How do you compare 40ms on seconds, while before you take 120ms and take a third (40ms)?</p>
<p>A third of a few seconds (say 6) is still (2) seconds gain.</p>
<p>Instead of:</p>
<p>for ($i = 1933999; $i &gt; 0; $i&#8211;) {<br />
&nbsp;&nbsp;// do some stuff<br />
}</p>
<p>You could use a Duff&#8217;s device and save some seconds. I benchmarked it with real functions and gained a third in speed. </p>
<p>So, if it doesn&#8217;t seems worth the trouble&#8230; don&#8217;t use it.</p>
<p>- Unomi -</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arnold Daniels</title>
		<link>http://www.jasny.net/articles/improve-performace-check-your-loops/comment-page-1/#comment-9851</link>
		<dc:creator>Arnold Daniels</dc:creator>
		<pubDate>Mon, 28 Jan 2008 22:58:18 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/improve-performace-check-your-loops/#comment-9851</guid>
		<description>The code for the loop might be 3x faster, but probably that isn&#039;t in comparison to the processing time of //do some stuff. So in the end I don&#039;t think this will actually help you a lot when you are having performance problems.

&lt;?php
&#160;&#160;$time = microtime(true);
&#160;&#160;for ($i=0; $i&lt;1000000; $i++) {
&#160;&#160;&#160;&#160;// Do nothing
&#160;&#160;}
&#160;&#160;echo (microtime(true) - $time) * 1000, &#039; ms&#039;;
?&gt;

This script will take about 120ms to run on my computer. A third of that is 40ms. If I replace //Do nothing with something simple as $s .= $i;, the script will take about 500ms.

A script that actually has a function will probably take a few seconds to process a million items. Saving 40ms than hardly seems worth the trouble.</description>
		<content:encoded><![CDATA[<p>The code for the loop might be 3x faster, but probably that isn&#8217;t in comparison to the processing time of //do some stuff. So in the end I don&#8217;t think this will actually help you a lot when you are having performance problems.</p>
<p>< ?php<br />
&nbsp;&nbsp;$time = microtime(true);<br />
&nbsp;&nbsp;for ($i=0; $i&lt;1000000; $i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;// Do nothing<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;echo (microtime(true) - $time) * 1000, ' ms';<br />
?></p>
<p>This script will take about 120ms to run on my computer. A third of that is 40ms. If I replace //Do nothing with something simple as $s .= $i;, the script will take about 500ms.</p>
<p>A script that actually has a function will probably take a few seconds to process a million items. Saving 40ms than hardly seems worth the trouble.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Unomi</title>
		<link>http://www.jasny.net/articles/improve-performace-check-your-loops/comment-page-1/#comment-9841</link>
		<dc:creator>Unomi</dc:creator>
		<pubDate>Mon, 28 Jan 2008 21:55:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/improve-performace-check-your-loops/#comment-9841</guid>
		<description>Also try to figure out what will be processed presumably the most of a switch / if statement. Put that as the first statement. Second most second etc.

Do a for loop with an iterator backwards ($i++ will be $i--), it&#039;s slightly faster. If you&#039;re desperate about your loop, use a Duff&#039;s device or a revision of it. Like this:

$i = 1933999; // any amount
$n = (int)($i % 8); // first do a modulo
$c = 0; // just to check the result

if ($n &gt; 0) {
&#160;&#160;while ($n--) {
&#160;&#160;&#160;&#160;//do some stuff
&#160;&#160;&#160;&#160;$c++;
&#160;&#160;}
}

$n = (int)($i / 8);

if ($n &gt; 0) {
&#160;&#160;while ($n--) {
&#160;&#160;&#160;&#160;//do some stuff
&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;//do some stuff
&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;//do some stuff
&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;//do some stuff
&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;//do some stuff
&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;//do some stuff
&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;//do some stuff
&#160;&#160;&#160;&#160;$c++;
&#160;&#160;&#160;&#160;//do some stuff
&#160;&#160;&#160;&#160;$c++;
&#160;&#160;}
}

It is much more code than usual, but it works like 1/3 faster than a normal loop. You have to do the last stuff 8 times, but only if $n &gt; 0; The first does a modulo, so everything not dividable by 8 comes first. The remaining comes after that.

You could even do query results this way, but I won&#039;t go into that.

For people saying it is less readable, I agree. But you could get used to it and take this method for faster results.

- Unomi -</description>
		<content:encoded><![CDATA[<p>Also try to figure out what will be processed presumably the most of a switch / if statement. Put that as the first statement. Second most second etc.</p>
<p>Do a for loop with an iterator backwards ($i++ will be $i&#8211;), it&#8217;s slightly faster. If you&#8217;re desperate about your loop, use a Duff&#8217;s device or a revision of it. Like this:</p>
<p>$i = 1933999; // any amount<br />
$n = (int)($i % 8); // first do a modulo<br />
$c = 0; // just to check the result</p>
<p>if ($n &gt; 0) {<br />
&nbsp;&nbsp;while ($n&#8211;) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;//do some stuff<br />
&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;}<br />
}</p>
<p>$n = (int)($i / 8);</p>
<p>if ($n &gt; 0) {<br />
&nbsp;&nbsp;while ($n&#8211;) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;//do some stuff<br />
&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;//do some stuff<br />
&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;//do some stuff<br />
&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;//do some stuff<br />
&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;//do some stuff<br />
&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;//do some stuff<br />
&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;//do some stuff<br />
&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;//do some stuff<br />
&nbsp;&nbsp;&nbsp;&nbsp;$c++;<br />
&nbsp;&nbsp;}<br />
}</p>
<p>It is much more code than usual, but it works like 1/3 faster than a normal loop. You have to do the last stuff 8 times, but only if $n &gt; 0; The first does a modulo, so everything not dividable by 8 comes first. The remaining comes after that.</p>
<p>You could even do query results this way, but I won&#8217;t go into that.</p>
<p>For people saying it is less readable, I agree. But you could get used to it and take this method for faster results.</p>
<p>- Unomi -</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arnold Daniels</title>
		<link>http://www.jasny.net/articles/improve-performace-check-your-loops/comment-page-1/#comment-9535</link>
		<dc:creator>Arnold Daniels</dc:creator>
		<pubDate>Fri, 25 Jan 2008 01:10:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/improve-performace-check-your-loops/#comment-9535</guid>
		<description>I can agree with you up to some point. Yes the it won&#039;t make the code more beautiful (butt ugly is a better term) and it will perform worse than code that is parsed normally, however you shouldn&#039;t dismiss it so quickly.

You are talking about a 1 million record loop. That is exactly the kind of big loops I&#039;m taking about. Within such a loop there should be as little as possible. No big if or switch statements, no calling all kind of abstract functions and most definitely no using expensive stuff like create_function(). If you&#039;re able to do that without use create_function, good! use that. However, if I have the choice between putting lots of stuff in the loop and having performance problems, or preparing for the loop and using create_function and have good performance, I choose the latter.

The fact that code like this might end up anywhere is not really a valid statement. You&#039;ll use this is at a point where you&#039;re going to loop through tens of thousands (or more) rows, It is highly unlikely that that code is in yet another big loop. 

Last, the example given, of creating an HTML table, is quite reusable. It works for any result set, simply pass data and meta-data.</description>
		<content:encoded><![CDATA[<p>I can agree with you up to some point. Yes the it won&#8217;t make the code more beautiful (butt ugly is a better term) and it will perform worse than code that is parsed normally, however you shouldn&#8217;t dismiss it so quickly.</p>
<p>You are talking about a 1 million record loop. That is exactly the kind of big loops I&#8217;m taking about. Within such a loop there should be as little as possible. No big if or switch statements, no calling all kind of abstract functions and most definitely no using expensive stuff like create_function(). If you&#8217;re able to do that without use create_function, good! use that. However, if I have the choice between putting lots of stuff in the loop and having performance problems, or preparing for the loop and using create_function and have good performance, I choose the latter.</p>
<p>The fact that code like this might end up anywhere is not really a valid statement. You&#8217;ll use this is at a point where you&#8217;re going to loop through tens of thousands (or more) rows, It is highly unlikely that that code is in yet another big loop. </p>
<p>Last, the example given, of creating an HTML table, is quite reusable. It works for any result set, simply pass data and meta-data.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ctx2002</title>
		<link>http://www.jasny.net/articles/improve-performace-check-your-loops/comment-page-1/#comment-9529</link>
		<dc:creator>ctx2002</dc:creator>
		<pubDate>Fri, 25 Jan 2008 00:28:13 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/improve-performace-check-your-loops/#comment-9529</guid>
		<description>have you read user&#039;s comments under that page?

&quot;You really should avoid using this(create_function) as well as you should avoid using eval(). Not only will there be a performance decrease but can it lead to obfuscation and bad coding habits. There is almost always an alternative solution to self modifying code.&quot; 

&quot;Beware when using anonymous functions in PHP as you would in languages like Python, Ruby, Lisp or Javascript.  As was stated previously, the allocated memory is never released; they are not objects in PHP -- they are just dynamically named global functions -- so they don&#039;t have scope and are not subject to garbage collection.

So, if you&#039;re developing anything remotely reusable (OO or otherwise), I would avoid them like the plague.  They&#039;re slow, inefficient and there&#039;s no telling if your implementation will end up in a large loop.  Mine ended up in an iteration over ~1 million records and quickly exhasted my 500MB-per-process limit&quot;

also check out http://blog.libssh2.org/index.php?/archives/60-create_function-is-not-your-friend.html

regards</description>
		<content:encoded><![CDATA[<p>have you read user&#8217;s comments under that page?</p>
<p>&#8220;You really should avoid using this(create_function) as well as you should avoid using eval(). Not only will there be a performance decrease but can it lead to obfuscation and bad coding habits. There is almost always an alternative solution to self modifying code.&#8221; </p>
<p>&#8220;Beware when using anonymous functions in PHP as you would in languages like Python, Ruby, Lisp or Javascript.  As was stated previously, the allocated memory is never released; they are not objects in PHP &#8212; they are just dynamically named global functions &#8212; so they don&#8217;t have scope and are not subject to garbage collection.</p>
<p>So, if you&#8217;re developing anything remotely reusable (OO or otherwise), I would avoid them like the plague.  They&#8217;re slow, inefficient and there&#8217;s no telling if your implementation will end up in a large loop.  Mine ended up in an iteration over ~1 million records and quickly exhasted my 500MB-per-process limit&#8221;</p>
<p>also check out <a href="http://blog.libssh2.org/index.php?/archives/60-create_function-is-not-your-friend.html" rel="nofollow">http://blog.libssh2.org/index.php?/archives/60-create_function-is-not-your-friend.html</a></p>
<p>regards</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arnold Daniels</title>
		<link>http://www.jasny.net/articles/improve-performace-check-your-loops/comment-page-1/#comment-9518</link>
		<dc:creator>Arnold Daniels</dc:creator>
		<pubDate>Thu, 24 Jan 2008 21:59:37 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/improve-performace-check-your-loops/#comment-9518</guid>
		<description>Sure, but this isn&#039;t really a problem. This will only cause you a few milliseconds, hardly something to worry about. Again, I&#039;m not a performance purist, removing all abstraction (so not using field definitions) will give you even better results. However if you loop through a large set of data and still want to use the abstraction, this will give you good results. And that is what this article is about, solving performance problems, not trying to squeeze the best performance out of any script. To my opinion, that is nonsense.</description>
		<content:encoded><![CDATA[<p>Sure, but this isn&#8217;t really a problem. This will only cause you a few milliseconds, hardly something to worry about. Again, I&#8217;m not a performance purist, removing all abstraction (so not using field definitions) will give you even better results. However if you loop through a large set of data and still want to use the abstraction, this will give you good results. And that is what this article is about, solving performance problems, not trying to squeeze the best performance out of any script. To my opinion, that is nonsense.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Edward Z. Yang</title>
		<link>http://www.jasny.net/articles/improve-performace-check-your-loops/comment-page-1/#comment-9515</link>
		<dc:creator>Edward Z. Yang</dc:creator>
		<pubDate>Thu, 24 Jan 2008 21:41:48 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/improve-performace-check-your-loops/#comment-9515</guid>
		<description>What ctx2002 is referring to is the fact that when the PHP interpreter hits a create_function block, it needs to shift back into parser mode, parse the PHP code, generate the opcodes, and then execute them.</description>
		<content:encoded><![CDATA[<p>What ctx2002 is referring to is the fact that when the PHP interpreter hits a create_function block, it needs to shift back into parser mode, parse the PHP code, generate the opcodes, and then execute them.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
