<?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>C syntax &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/c-syntax/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Beginning Algorithm Complexity and Estimation</title>
		<link>/2010/08/29/beginning-algorithm-complexity-and-estimation/</link>
		<comments>/2010/08/29/beginning-algorithm-complexity-and-estimation/#respond</comments>
		<pubDate>Sun, 29 Aug 2010 11:05:47 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[Analysis of algorithms]]></category>
		<category><![CDATA[Asymptotic analysis]]></category>
		<category><![CDATA[Big O notation]]></category>
		<category><![CDATA[C syntax]]></category>
		<category><![CDATA[complexity]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[IP]]></category>
		<category><![CDATA[Lenstra elliptic curve factorization]]></category>
		<category><![CDATA[Mathematical analysis]]></category>
		<category><![CDATA[Mathematical notation]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Negation]]></category>
		<category><![CDATA[programmer]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Variable]]></category>

		<guid isPermaLink="false">/?p=1937</guid>
		<description><![CDATA[Which is the Fastest Program? When a programmer sees a chunk of code he tends to evaluate it in a rather intuitive manner and to qualify it as &#8220;elegant&#8221; or not. This is quite easy, because it&#8217;s subjective and nobody knows what exactly elegant means. However behind this there is a powerful mathematical approach of &#8230; <a href="/2010/08/29/beginning-algorithm-complexity-and-estimation/" class="more-link">Continue reading <span class="screen-reader-text">Beginning Algorithm Complexity and Estimation</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/09/03/friday-algorithms-input-data-and-complexity/" rel="bookmark" title="Friday Algorithms: Input Data and Complexity">Friday Algorithms: Input Data and Complexity </a></li>
<li><a href="/2010/10/03/using-php-array_diff-in-algorithm-development/" rel="bookmark" title="Using PHP&#8217;s array_diff in Algorithm Development">Using PHP&#8217;s array_diff in Algorithm Development </a></li>
<li><a href="/2011/11/04/how-to-check-if-a-date-is-more-or-less-than-a-month-ago-with-php/" rel="bookmark" title="How to Check if a Date is More or Less Than a Month Ago with PHP">How to Check if a Date is More or Less Than a Month Ago with PHP </a></li>
<li><a href="/2012/03/12/algorithm-cheatsheet-quicksort/" rel="bookmark" title="Algorithm cheatsheet: Quicksort">Algorithm cheatsheet: Quicksort </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Which is the Fastest Program?</h2>
<p><a href="/wp-content/uploads/2010/08/complexity.jpg"><img src="/wp-content/uploads/2010/08/complexity.jpg" alt="" title="circuit" width="430" height="213" class="aligncenter size-full wp-image-1949" srcset="/wp-content/uploads/2010/08/complexity.jpg 430w, /wp-content/uploads/2010/08/complexity-300x148.jpg 300w" sizes="(max-width: 430px) 100vw, 430px" /></a><br />
When a programmer sees a chunk of code he tends to evaluate it in a rather intuitive manner and to qualify it as &#8220;elegant&#8221; or not. This is quite easy, because it&#8217;s subjective and nobody knows what exactly elegant means. However behind this there is a powerful mathematical approach of measuring a program effectiveness.</p>
<p>It&#8217;s a pity that most of the developers still think of the big O notation as something from the university classes, but unusual in the practice and they barely use it their job. But before describing the big O notation, let me start from something really simple.</p>
<p>Let&#8217;s have the following example (note that all the examples are in PHP):</p>
<pre lang="php">
$n = 100;
$s = 0;

for ($i = 0; $i < $n; $i++) {
	for ($j = 0; $j < $n; $j++) {
		$s++;	
	}	
}
</pre>
<p>As you can see there are two assignments and two nested loops. This is really a widely used example from any algorithm book.</p>
<h2>Constants, Languages, Compilers</h2>
<p>First of all the time to assign a value to a variable, to compare two values and to increment a variable is constant. It depends on the computer resources, the compiler or the language, but it's constant on one machine if you compare two chunks of code. Now we can see that these operations take (add) constant time to the program, and we can assume this time is respectively a, b, c, d, e, f, g, h, i.</p>
<pre lang="php">
$n = 100; 	// a
$s = 0;		// b
$i = 0; 	// c
$i < $n; 	// d
$i++;		// e
$j = 0; 	// f
$j < $n;	// g
$j++;		// h
$s++;		// i
</pre>
<h2>What Matters?</h2>
<p>Actually the most important thing here is the value of n. By assigning greater values to n the more time will take the program to run. As we can see from the following table by multiplying the value of n by 10, the time became 100 times more.</p>
<pre lang="php">
n		time
10		0.00002
100		0.002
...		...
</pre>
<p>What happens in fact is that we can sum all these values.</p>
<pre lang="php">
a + b + c + n*d + n*e + n*(f + n*g + n*h + n*i)
</pre>
<p>and by substituting:</p>
<pre lang="php">
a + b + c = k
d + e + n = l
g + h + i = m
</pre>
<p>the result is:</p>
<pre lang="php">
m*n² + l*n + k
</pre>
<h2>Conclusion</h2>
<p>Here the most important thing is the degree of n, because it can change dramatically the program time consumption depending on the n value. Thus this chunk has a quadratic complexity or O(n²).</p>
<p>Of course there are constants, but in the practice they are not so important. Take a look at these two functions:</p>
<pre lang="php">
f = 2*n²
g = 200*n
</pre>
<p>OK, for n = 1 the first one will be faster, but as n increments the second function becomes to be faster and faster, thus after a given value of n the second function is really the fastest!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/09/03/friday-algorithms-input-data-and-complexity/" rel="bookmark" title="Friday Algorithms: Input Data and Complexity">Friday Algorithms: Input Data and Complexity </a></li>
<li><a href="/2010/10/03/using-php-array_diff-in-algorithm-development/" rel="bookmark" title="Using PHP&#8217;s array_diff in Algorithm Development">Using PHP&#8217;s array_diff in Algorithm Development </a></li>
<li><a href="/2011/11/04/how-to-check-if-a-date-is-more-or-less-than-a-month-ago-with-php/" rel="bookmark" title="How to Check if a Date is More or Less Than a Month Ago with PHP">How to Check if a Date is More or Less Than a Month Ago with PHP </a></li>
<li><a href="/2012/03/12/algorithm-cheatsheet-quicksort/" rel="bookmark" title="Algorithm cheatsheet: Quicksort">Algorithm cheatsheet: Quicksort </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/08/29/beginning-algorithm-complexity-and-estimation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: What is More Powerful Than list()</title>
		<link>/2010/08/27/php-what-is-more-powerful-than-list/</link>
		<comments>/2010/08/27/php-what-is-more-powerful-than-list/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 12:18:39 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Binary search algorithm]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[C syntax]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Data types]]></category>
		<category><![CDATA[Google Inc.]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[paging]]></category>
		<category><![CDATA[search result page]]></category>
		<category><![CDATA[search results]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Type theory]]></category>
		<category><![CDATA[Variables]]></category>
		<category><![CDATA[web system]]></category>

		<guid isPermaLink="false">/?p=1933</guid>
		<description><![CDATA[First of all list() is not an unknown method in the PHP community, where almost every PHP developer knows what it does. The pity is that it has, perhaps, remained useless, although there is hidden power in it! What is list()? Let&#8217;s assume you&#8217;ve two variables and one array with two elements: $arr = array(1, &#8230; <a href="/2010/08/27/php-what-is-more-powerful-than-list/" class="more-link">Continue reading <span class="screen-reader-text">PHP: What is More Powerful Than list()</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/08/31/php-what-is-more-powerful-than-list-perhaps-extract/" rel="bookmark" title="PHP: What is More Powerful Than list() &#8211; Perhaps extract()">PHP: What is More Powerful Than list() &#8211; Perhaps extract() </a></li>
<li><a href="/2010/09/29/construct-a-sorted-php-linked-list/" rel="bookmark" title="Construct a Sorted PHP Linked List">Construct a Sorted PHP Linked List </a></li>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>First of all <a title="PHP list" href="http://php.net/manual/en/function.list.php" target="_blank">list()</a> is not an unknown method in the PHP community, where almost every PHP developer knows what it does. The pity is that it has, perhaps, remained useless, although there is hidden power in it!</p>
<h2>What is list()?</h2>
<p>Let&#8217;s assume you&#8217;ve two variables and one array with two elements:</p>
<pre lang="php">
$arr = array(1, 2);
$a = $arr[0];
$b = $arr[1];
// now a == 1, and b == 2
</pre>
<p>Maybe the easiest way to assign to the first variable the value of the first element of the array, and to the second variable the value of the second element of the array is to use list():</p>
<pre lang="php">
list($a, $b) = array(1, 2);
// again a == 1, and b == 2
</pre>
<p>Note that you can do the same with as many variables/array elements you want.</p>
<p>The funny thing is that nobody use it, while I see at least one perfect usage. You can think of a typical admin panel of any web system. For sure any developer has programmed the typical table page containing a list of &#8220;things&#8221; and a paging to switch the result set page. It&#8217;s like the posts page from the WordPress admin panel, or a search result page in Google, where you&#8217;ve one page of results and at the bottom &#8211; a number of pages where you can go to another page of the search results.</p>
<p>What I&#8217;ve seen in almost every project is that typically the developers prefer to set the results for the page into one list returned from one method and the count of the entire result set is returned by another method.</p>
<p>Thus most of the times there are two methods, performing most commonly a database queries. Something like &#8211; getList($offset, $limit) and getCount() where the first one returns the page and the second one returns the count of the result set.</p>
<p>This will result into two methods, two calls and two lines of code:</p>
<pre lang="php">
function getList($offset, $limit)
{
	...
	// although the entire result set contains
	// 5 elements by limiting it from the parameters
	// only three are returned
	return $list; // where $list = array('title1', 'title2', 'title3');	
}

function getCount()
{
	...
	// here's returned the count
	// of the entire result set
	return $count; // where $count = 5;	
}

$list  = getList(0, 3);
$count = getCount();
</pre>
<p>What actually you can do is to perform both queries in one method, perhaps called getItems($offset, $limit) where the returned value is an array containing both the list of results and the count:</p>
<pre lang="php">
function getItems($offset, $limit)
{
	...
	return array('list' => $list, 'count' => $count);
}
</pre>
<p>And finally when calling this method to list() that into the two variables &#8211; single lined.</p>
<pre lang="php">
list($list, $count) = getItems(0, 3);
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/08/31/php-what-is-more-powerful-than-list-perhaps-extract/" rel="bookmark" title="PHP: What is More Powerful Than list() &#8211; Perhaps extract()">PHP: What is More Powerful Than list() &#8211; Perhaps extract() </a></li>
<li><a href="/2010/09/29/construct-a-sorted-php-linked-list/" rel="bookmark" title="Construct a Sorted PHP Linked List">Construct a Sorted PHP Linked List </a></li>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/08/27/php-what-is-more-powerful-than-list/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
