<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DOC776.org &#187; Fact</title>
	<atom:link href="http://www.doc776.org/category/fact/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.doc776.org</link>
	<description>This is the era of information.</description>
	<lastBuildDate>Mon, 02 Jan 2012 00:05:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Getting Directory Tree &#8211; Recursive VS Non-Recursive</title>
		<link>http://www.doc776.org/2009/07/getting-directory-tree-recursive-vs-non-recursive/</link>
		<comments>http://www.doc776.org/2009/07/getting-directory-tree-recursive-vs-non-recursive/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 22:33:48 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Fact]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[info]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[recursive]]></category>

		<guid isPermaLink="false">http://www.doc776.org/?p=162</guid>
		<description><![CDATA[I took some time to examine between the performance of using a recursive and non recursive method of retrieving a directory tree. Of-course retrieving a directory tree is possible with out using any functions and still be able to get the full tree with unlimited depth possibilities. I made the non recursive piece of code [...]]]></description>
			<content:encoded><![CDATA[<p>I took some time to examine between the performance of using a recursive and non recursive method of retrieving a directory tree. Of-course retrieving a directory tree is possible with out using any functions and still be able to get the full tree with unlimited depth possibilities. I made the non recursive piece of code that outputs an array of each file and folder/subfolder of a given directory and a recursive function that calls it self on each new folder found. And then before an after I took a memory usage survey and time lapse of code execution. The folder that i used for testnig had some random folders and files and a freshly downloaded codeigniter frame work. So there were about 360 items in the generated array of files and folders.</p>
<p><strong>Non-Recursive Code:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$tree</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'folder/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$folders_to_do</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'folder/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$current_folder</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$folders_to_do</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$key_of_current_folder</span> <span style="color: #339933;">=</span> <span style="color: #990000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$folders_to_do</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$current_folder</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$folders_to_do</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key_of_current_folder</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$folder</span> <span style="color: #339933;">=</span> <span style="color: #990000;">scandir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_folder</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$folder</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_folder</span><span style="color: #339933;">.</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> and <span style="color: #000088;">$item</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'.'</span> and <span style="color: #000088;">$item</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'..'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$folders_to_do</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$current_folder</span><span style="color: #339933;">.</span><span style="color: #000088;">$item</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$tree</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$current_folder</span><span style="color: #339933;">.</span><span style="color: #000088;">$item</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_folder</span><span style="color: #339933;">.</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000088;">$tree</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$current_folder</span><span style="color: #339933;">.</span><span style="color: #000088;">$item</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$folders_to_do</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key_of_current_folder</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tree</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The way it works is very simple. It runs only on 2 loops (while &#038; foreach), 1 array (folders_to_do) and 1 string variable (current_folder) to control what we are scanning. The outer loop will always loop it self as long as it has something to scan, that means if the to-do array is not empty it will loop again. Now inside the while loop we have the regular scandir() function and foreach loop. in the foreach loop when a file is found it is added to the main tree array but if a folder is found then it is not only added to the tree array but is also added to the to-do list. Since the to-do list has something new to scan, the while loop will not brake. At the end when there is no more directories to scan the tree array will contain every folder and file found in the initial directory passed. All that is left to do is to sort the array and you get a neat list of each files and folders of a passed directory.</p>
<p><strong>Recursive:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$tree2</span> <span style="color: #339933;">=</span> recursive<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'folder/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> recursive<span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_folder</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	static <span style="color: #000088;">$tree</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$folder</span> <span style="color: #339933;">=</span> <span style="color: #990000;">scandir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_folder</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$folder</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_folder</span><span style="color: #339933;">.</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> and <span style="color: #000088;">$item</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'.'</span> and <span style="color: #000088;">$item</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'..'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$tree</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$current_folder</span><span style="color: #339933;">.</span><span style="color: #000088;">$item</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">;</span>
			recursive<span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_folder</span><span style="color: #339933;">.</span><span style="color: #000088;">$item</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_folder</span><span style="color: #339933;">.</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000088;">$tree</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$current_folder</span><span style="color: #339933;">.</span><span style="color: #000088;">$item</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$tree</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This method is also straight forward. It uses a static array inside the function to hold the tree, optionally you can keep passing the tree as an argument if you do not wish to use a static array or if you simple going to use the function multiple times on the same script. The function it self takes in only 1 string variable which is the folder it is going to process. The foreach loop does same thing basically; if it is a file it only adds to the tree array and if it is a folder it adds to the tree array and calls the function with the folder path as teh argument. The recursion concept is no different then any other. It was only simplified ridiculously.</p>
<p><strong>Result Array:</strong></p>
<pre>
Array
(
    [0] => folder/
    [1] => folder/another folder/
    [2] => folder/another folder/bar file
    [3] => folder/another folder/dddddd/
    [4] => folder/another folder/dddddd/lost track/
    [5] => folder/another folder/dddddd/lost track/last file
    [6] => folder/another folder/hhh/
    [7] => folder/another omg folder/
    [8] => folder/codeigniter omg/
    [9] => folder/codeigniter omg/index.php
...
</pre>
<p>Both of those codes will produce exactly the same thing but using different amount of memory. Above you can see a steddy array with each folder and file name with full paths.</p>
<p><strong>Performance:</strong><br />
AS I said earlier I had about 360 files and folder combined for testing.</p>
<p>The time in milliseconds took to execute was the same on average of those methods. I refreshed many times and it was always either same or one or the other had few more milliseconds.</p>
<p>The memory usage for the non recursive was 82,512 and for the recursive method it was 116,112. I used the memory_get_usage() function to find those numbers. For each method I took memory usage before the code and after the code. So the results are (after_code &#8211; before_code) for first and second methods, just to clarify so no one has questions to how I have come to my conclusion.</p>
<p>82,512 / 116,112 = 0.71 ratio!</p>
<p>All testing was done here: http://www.sandbox.doc776.org/recursion/script.php</p>
]]></content:encoded>
			<wfw:commentRss>http://www.doc776.org/2009/07/getting-directory-tree-recursive-vs-non-recursive/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why We Should Eat Right and Stay Fit</title>
		<link>http://www.doc776.org/2008/07/why-we-should-eat-right-and-stay-fit/</link>
		<comments>http://www.doc776.org/2008/07/why-we-should-eat-right-and-stay-fit/#comments</comments>
		<pubDate>Tue, 29 Jul 2008 02:52:01 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Fact]]></category>
		<category><![CDATA[Humor]]></category>
		<category><![CDATA[health]]></category>

		<guid isPermaLink="false">http://www.doc776.org/?p=90</guid>
		<description><![CDATA[Written by Joe Every year, countless lives are lost to heart failure. The cause of this failure is the extra strain on the heart due to lack of exercise and good eating habits. When people decide they don&#8217;t have time or money in their lives to take care of their health by eating right and [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: right;">Written by Joe</p>
<p>Every year, countless lives are lost to heart failure. The cause of this failure is the extra strain on the heart due to lack of exercise and good eating habits.</p>
<p><img class="aligncenter size-full wp-image-96" title="heart" src="http://www.doc776.org/wp-content/uploads/heart.png" alt="heart" width="277" height="208" /></p>
<p><span id="more-90"></span>When people decide they don&#8217;t have time or money in their lives to take care of their health by eating right and staying in shape, they ignore the symptoms which precursor death at an early age. They learn to have an affair with the family couch and take advantage of McDonald&#8217;s and BurgerKing&#8217;s dollar menus. The food they buy from these wretched places is poison. It contains next to no nutritional value. Yeah, it may give your tongue pleasure (sometimes), and can give you energy for a period of time and keep you alive, but that&#8217;s all.</p>
<p><img class="aligncenter size-full wp-image-92" title="burgetking" src="http://www.doc776.org/wp-content/uploads/burgetking.png" alt="burgetking" width="310" height="233" /></p>
<p><img class="alignleft size-medium wp-image-91" title="burgerkingcrown" src="http://www.doc776.org/wp-content/uploads/burgerkingcrown-300x127.png" alt="burgerkingcrown" width="300" height="127" />As if buying your own death due to time and money constraints wasn&#8217;t bad enough, the companies behind these genocidal brands have extremely slick marketing techniques. They make their King and Clown household names. They give young kids action figures so they can grow up knowing that these worthless “restaurants” are always available for a quick and cheap “meal”. Eating this food throughout a lifetime can lead to diabetes, heart disease, high cholesterol, high blood pressure, and other illnesses.</p>
<p>Most people know, or at least have heard of, the risks involved in consuming this food. They know it can drastically reduce lifespan. The problem is that it is human instinct to be overly optimistic. What society really needs is an army of pessimists running wild on the streets advocating healthy foods. People go into a Burger King wielding the attitude that one burger can&#8217;t hurt them. It won&#8217;t, not them. God loves them too much. This “one burger” quickly turns into a routine, which can consist of many consumptions per week and can lead to an early fatality. What the stock holders of these companies have devised is genocide, in a painstakingly slow way.</p>
<p><img class="aligncenter size-full wp-image-93" title="eaters" src="http://www.doc776.org/wp-content/uploads/eaters.png" alt="eaters" width="681" height="209" /></p>
<p>Anyone reading this might believe at this point that all they need to do is avoid fast food to live a normal life span to avoid the risks of heart attacks, strokes, etc. They are dead wrong. In addition to avoiding worthless “food,” people need to exercise.</p>
<p><img class="aligncenter size-full wp-image-94" title="exercise" src="http://www.doc776.org/wp-content/uploads/exercise.png" alt="exercise" width="681" height="209" /></p>
<p>Exercising on a regular basis is the key to living a longer life. Not only is there improved life expectancy, having a healthy heart provides an ample amount of blood to the body&#8217;s main thinking center.</p>
<p><img class="aligncenter size-full wp-image-97" title="thinkingcenter" src="http://www.doc776.org/wp-content/uploads/thinkingcenter.png" alt="thinkingcenter" width="574" height="178" /></p>
<p>And lastly, the most noticeable aspect of eating right and staying in shape is increased physical attraction. In certain cases this can be good or bad. It can obviously be good because it creates a window of opportunity for one to become more social, add more to the human population, and be less of an eyesore to most people. The bad part is that people can be be so jealous of you to the point of wanting to kill you.</p>
<p style="text-align: center;">Below are examples of people who eat healthy and stay in shape:</p>
<p><img class="aligncenter size-full wp-image-95" title="healthyones" src="http://www.doc776.org/wp-content/uploads/healthyones.png" alt="healthyones" width="681" height="209" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.doc776.org/2008/07/why-we-should-eat-right-and-stay-fit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

