<?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>Asymptotic analysis &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/asymptotic-analysis/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>
	</channel>
</rss>
