<?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>Huddled Masses &#187; Timing</title>
	<atom:link href="http://huddledmasses.org/tag/timing/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Recursion in PowerShell &#8230; slow</title>
		<link>http://huddledmasses.org/recursion-in-powershell-slow/</link>
		<comments>http://huddledmasses.org/recursion-in-powershell-slow/#comments</comments>
		<pubDate>Mon, 04 Feb 2008 06:12:20 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Fibonacci]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Recursion]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Timing]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/recursion-in-powershell-slow/</guid>
		<description><![CDATA[So, I posted a story about Fibonacci sequences and noticed that the recursion in PowerShell is really rather slow. But I also noticed something interesting &#8212; I had expected that recursing using a function would be a little cheaper (and faster) than recursing using the pipeline, and it turns out that it&#8217;s not. At all. [...]]]></description>
			<content:encoded><![CDATA[	<p>So, I posted <a href="http://huddledmasses.org/fibbonaci-sequence-in-powershell/">a story about Fibonacci sequences</a> and noticed that the recursion in PowerShell is really rather slow. But I also noticed something interesting &#8212; I had expected that recursing using a function would be a little cheaper (and faster) than recursing using the pipeline, and it turns out that it&#8217;s not. At all.  </p>

	<p>I&#8217;d love to hear some thoughts on why I got the speed results I got here.  At first I thought it was something about type coercion, but as you can see, I tried casting everything to no avail.  There&#8217;s no difference if I don&#8217;t output anything, and in fact, the non-recursive algorithm can trivially output every Fibonacci number in the sequence without affecting it&#8217;s time (I actually altered it so it didn&#8217;t to keep the output clean).</p>

	<p>Obviously I expected the recursive algorithms to be slow and scale badly because of the amount of work involved, but how is it that the pipeline-recursive &#8220;filter&#8221; outperforms the plain old recursive function (albeit by an infinitesimal fraction)?</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #333;">&#91;</span>08<span style="color: #333;">&#93;</span>: <span style="color: #666699; font-weight: bold;">function</span> fibfunction<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #000066;">-lt</span> <span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #cc66cc;">1</span> <span style="color: #333;">&#125;</span><span style="color: #666699; font-weight: bold;">else</span><span style="color: #333;">&#123;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#40;</span>fibfunction <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#40;</span>fibfunction <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span> <span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #333;">&#91;</span>09<span style="color: #333;">&#93;</span>: <span style="color: #666699; font-weight: bold;">filter</span> fibfilter<span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #66cc66;">-</span>lt2<span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#125;</span><span style="color: #666699; font-weight: bold;">else</span><span style="color: #333;">&#123;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">|</span>fibfilter<span style="color: #333;">&#41;</span><span style="color: #66cc66;">+</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">|</span>fibfilter<span style="color: #333;">&#41;</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">10</span><span style="color: #333;">&#93;</span>: <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$size</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">18</span><br />
<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">11</span><span style="color: #333;">&#93;</span>: fibfunction <span style="color: #660033; font-weight: bold;">$size</span><br />
<span style="color: #cc66cc;">4181</span><br />
<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">12</span><span style="color: #333;">&#93;</span>: <span style="color: #660033; font-weight: bold;">$size</span> <span style="color: #66cc66;">|</span> fibfilter<br />
<span style="color: #cc66cc;">4181</span><br />
<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">13</span><span style="color: #333;">&#93;</span>: <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$a</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$b</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>; 0..<span style="color: #660033; font-weight: bold;">$size</span><span style="color: #66cc66;">|%</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$a</span>,<span style="color: #660033; font-weight: bold;">$b</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$a</span><span style="color: #66cc66;">+</span><span style="color: #660033; font-weight: bold;">$b</span><span style="color: #333;">&#41;</span>,<span style="color: #660033; font-weight: bold;">$a</span><span style="color: #333;">&#125;</span>; <span style="color: #660033; font-weight: bold;">$a</span><br />
<span style="color: #cc66cc;">4181</span><br />
<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">14</span><span style="color: #333;">&#93;</span>: <span style="color: #0066cc; font-style: italic;">get-<span style="font-style: normal;">history</span></span> <span style="color: #000066;">-count</span> <span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">fl</span> CommandLine,@<span style="color: #333;">&#123;</span>l<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Duration&quot;</span>; e<span style="color: #66cc66;">=</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">EndExecutionTime</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">StartExecutionTime</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><br />
<br />
CommandLine : fibfunction <span style="color: #660033; font-weight: bold;">$size</span><br />
Duration &nbsp; &nbsp;: 00:00:<span style="color: #cc66cc;">02.4764040</span><br />
<br />
CommandLine : <span style="color: #660033; font-weight: bold;">$size</span> <span style="color: #66cc66;">|</span> fibfilter<br />
Duration &nbsp; &nbsp;: 00:00:<span style="color: #cc66cc;">02.2137255</span><br />
<br />
CommandLine : <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$a</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$b</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>; 0..<span style="color: #660033; font-weight: bold;">$size</span><span style="color: #66cc66;">|%</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$a</span>,<span style="color: #660033; font-weight: bold;">$b</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$a</span><span style="color: #66cc66;">+</span><span style="color: #660033; font-weight: bold;">$b</span><span style="color: #333;">&#41;</span>,<span style="color: #660033; font-weight: bold;">$a</span><span style="color: #333;">&#125;</span>; <span style="color: #660033; font-weight: bold;">$a</span><br />
Duration &nbsp; &nbsp;: 00:00:<span style="color: #cc66cc;">00.0048825</span><br />
<br />
<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">15</span><span style="color: #333;">&#93;</span>: <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$size</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">21</span><br />
<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">16</span><span style="color: #333;">&#93;</span>: fibfunction <span style="color: #660033; font-weight: bold;">$size</span><br />
<span style="color: #cc66cc;">17711</span><br />
<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">17</span><span style="color: #333;">&#93;</span>: <span style="color: #660033; font-weight: bold;">$size</span> <span style="color: #66cc66;">|</span> fibfilter<br />
<span style="color: #cc66cc;">17711</span><br />
<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">18</span><span style="color: #333;">&#93;</span>: <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$a</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$b</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>; 0..<span style="color: #660033; font-weight: bold;">$size</span><span style="color: #66cc66;">|%</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$a</span>,<span style="color: #660033; font-weight: bold;">$b</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$a</span><span style="color: #66cc66;">+</span><span style="color: #660033; font-weight: bold;">$b</span><span style="color: #333;">&#41;</span>,<span style="color: #660033; font-weight: bold;">$a</span><span style="color: #333;">&#125;</span>; <span style="color: #660033; font-weight: bold;">$a</span><br />
<span style="color: #cc66cc;">17711</span><br />
<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">19</span><span style="color: #333;">&#93;</span>: <span style="color: #0066cc; font-style: italic;">get-<span style="font-style: normal;">history</span></span> <span style="color: #000066;">-count</span> <span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">fl</span> CommandLine,@<span style="color: #333;">&#123;</span>l<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Duration&quot;</span>; e<span style="color: #66cc66;">=</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">EndExecutionTime</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">StartExecutionTime</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><br />
<br />
CommandLine : <span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> fibfunction <span style="color: #660033; font-weight: bold;">$size</span><br />
Duration &nbsp; &nbsp;: 00:00:<span style="color: #cc66cc;">10.5764715</span><br />
<br />
CommandLine : <span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$size</span> <span style="color: #66cc66;">|</span> fibfilter<br />
Duration &nbsp; &nbsp;: 00:00:<span style="color: #cc66cc;">09.6282900</span><br />
<br />
CommandLine : <span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$a</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$b</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>; 0..<span style="color: #660033; font-weight: bold;">$size</span><span style="color: #66cc66;">|%</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$a</span>,<span style="color: #660033; font-weight: bold;">$b</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$a</span><span style="color: #66cc66;">+</span><span style="color: #660033; font-weight: bold;">$b</span><span style="color: #333;">&#41;</span>,<span style="color: #660033; font-weight: bold;">$a</span><span style="color: #333;">&#125;</span>; <span style="color: #660033; font-weight: bold;">$a</span><br />
Duration &nbsp; &nbsp;: 00:00:<span style="color: #cc66cc;">00.0058590</span></div>

	<p>P.S.: The numbers in braces with the colon (like [18]: ... ) is my prompt.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/recursion-in-powershell-slow/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

