<?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; Performance</title>
	<atom:link href="http://huddledmasses.org/tag/performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Fri, 27 Apr 2012 05:42:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>The SQL Server PowerShell Provider</title>
		<link>http://huddledmasses.org/the-sql-server-powershell-provider/</link>
		<comments>http://huddledmasses.org/the-sql-server-powershell-provider/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 17:00:32 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Provider]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/the-sql-server-powershell-provider/</guid>
		<description><![CDATA[This is an old post that&#8217;s been sitting in my queue for a long time, and I just finally thought I&#8217;d publish it, for what it&#8217;s worth. The word on the street was that SQL Server 2008&#8217;s PowerShell support is going to be limited. I&#8217;m here to say: it&#8217;s not so much limited, as it [...]]]></description>
			<content:encoded><![CDATA[	<p>This is an old post that&#8217;s been sitting in my queue for a long time, and I just finally thought I&#8217;d publish it, for what it&#8217;s worth.  The word on the street was that <span class="caps">SQL</span> Server 2008&#8217;s PowerShell support is going to be <a href="http://dmitrysotnikov.wordpress.com/2007/11/16/sql-powershell-details-revealed/">limited</a>.</p>

	<p>I&#8217;m here to say: it&#8217;s not so much limited, as it it cautiously restrained. A lot of people were apparently hoping for a <span class="caps">SQL</span> Server provider like <a href="http://blogs.msdn.com/powershell/archive/2007/06/21/demo-sql-provider-code.aspx">this demo one</a> that could fully navigate tables and list their contents etc., but I think the <span class="caps">SQL</span> Team <a href="http://blogs.msdn.com/powershell/archive/2007/11/13/sql-server-support-for-powershell.aspx">has decided</a> <strong>not</strong> to allow that, and I think they probably did the right thing.</p>

	<p>Honestly, a provider that can navigate into <span class="caps">SQL</span> datatables would be extremely frightening, to the point that it might need to be banned or blocked in some instances.  Just imagine a <span class="caps">SQL</span> Server dba who&#8217;s brand new to PowerShell, but thinks that new provider sounds interesting &#8230; so he fires up PowerShell and switches over to the customer orders table (you know, the one with 2 million records of purchases) and does:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033;">cd</span> SQL:\Server\Northwind\Orders<br />
<span style="color: #660033;">ls</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">where</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">ShipName</span> <span style="color: #000066;">-like</span> <span style="color: #009900;">&quot;Robert *&quot;</span> <span style="color: #333;">&#125;</span></div>

	<p>Just think about that line for a second. Not only is it fetching every record in the table, it has to build a .net object for every single one.  One of the most commonly used cmdlet&#8217;s in PowerShell is Get-Member, which we typically see used when you&#8217;re trying to figure out which properties are available on an item.  Applied to the previous example, you can imagine doing something like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033;">cd</span> SQL:\Server\Northwind\Orders<br />
<span style="color: #660033;">ls</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Member</span></span></div>

	<p>Now you&#8217;ve retrieved every record from the table &#8230; just for the sake of finding out what the columns are!  You&#8217;re not even using the data.  If you&#8217;re lucky, the memory use of creating 2 million System.Data.Row objects would cripple your dba&#8217;s PC before they were able to try something like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033;">cd</span> SQL:\Server\Northwind<br />
<span style="color: #660033;">ls</span> <span style="color: #66cc66;">|</span> <br />
&nbsp;<span style="color: #666699; font-weight: bold;">ForEach</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; <span style="color: #660033; font-weight: bold;">$Property</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">gm</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp;<span style="color: #660033;">where</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Name</span> <span style="color: #000066;">-like</span> <span style="color: #009900;">&quot;*Name&quot;</span> <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp;<span style="color: #660033;">Select</span> <span style="color: #000066;">-First</span> <span style="color: #cc66cc;">1</span>; <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #009900;">&quot;$($Property.Name)&quot;</span> <br />
<span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Where</span> <span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-like</span> <span style="color: #009900;">&quot;Robert*&quot;</span><span style="color: #333;">&#125;</span></div>

	<p>Anyway. Hopefully this counter example will be enough to not only make you feel better about using a cmdlet instead of a provider, but also to make some people think about how they choose to design things that can cause potentially crippling network traffic or server load.  The thing is that although it&#8217;s possible to allow the <code>-Filter</code> parameter of Get-ChildItem (ls) to specify both a <code>WHERE</code> clause and a <code>TOP</code> count &#8230; that wouldn&#8217;t help unless you were sure your users would always filter that way instead of by passing it through <code>| Where-Object</code>.  </p>

	<p>As a side note: The management studio protects you from this sort of load on your system to some extent by caching data and limiting how many rows it pulls over at a time, but not as much as it could if it allowed you to reorder columns and sort them after you had retrieved the data. It also has the distinct advantage of not necessarily having to create .Net objects to pass into the pipeline for each row on the fly (although, maybe that&#8217;s how it works, I don&#8217;t know, but I would hope not).</p>

	<p>Anyway, from what I&#8217;ve understood, the <span class="caps">SQL</span> powershell provider will let you navigate the schemas, and will provide some cmdlets to do queries that should replace some of the functions we&#8217;ve all been using to do queries. Maybe they&#8217;ll even provide something that can do built in paging, like: Get-SqlQuery &#8220;Select * from Northwind.Orders&#8221; -Next 10 &#8230;</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/the-sql-server-powershell-provider/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>Reservoir sampling and performance considerations</title>
		<link>http://huddledmasses.org/reservoir-sampling-and-performance-considerations/</link>
		<comments>http://huddledmasses.org/reservoir-sampling-and-performance-considerations/#comments</comments>
		<pubDate>Mon, 21 Jan 2008 04:25:41 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Pipeline]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Randomization]]></category>
		<category><![CDATA[Reservoir Sampling]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/reservoir-sampling-and-performance-considerations/</guid>
		<description><![CDATA[I started this post a few weeks ago, but never quite got around to posting it, and then I saw today that Dmitry wrote about the memory problems with using measure-object and I thought this would be a great time to post this. Essentially, this is a continuation of my exploration of development in the [...]]]></description>
			<content:encoded><![CDATA[	<p>I started this post a few weeks ago, but never quite got around to posting it, and then I saw today that <a href="http://dmitrysotnikov.wordpress.com/2008/01/18/measure-vs-count-vs-i/">Dmitry wrote about the memory problems with using measure-object</a> and I thought this would be a great time to post this.  Essentially, this is a continuation of my exploration of development in the PowerShell pipeline, and we&#8217;ll see some of the problems with the way that the pipeline works and what happens if you overlook the numbers of items that could be in the pipeline.</p>

	<p>Dmitry was trying to count the number of users in Active Directory, and ran into ridiculous memory use when using Measure-Object because it appears to collect all the items in the pipeline into memory before it counts, resulting in huge amounts of memory use based on the number of objects you&#8217;re counting.  Of course, for the purpose of counting, it&#8217;s a pretty obvious fix (as Dmitry explains), but for other purposes it may not be as obvious.</p>

	<h3>Select Random</h3>

	<p>When I originally started this post, I was trying to upgrade my original <a href="http://powershellcentral.com/scripts/60">Select-Random</a> script (which was published in the PowerShell Community Extensions) to use reservoir sampling in an attempt to solve the same problem.  Select-Random is a script which selects a random element from a collection or from the pipeline. In the past, if the elements were passed in as an argument, it simply collected them into an array (in a really inefficient way, but never mind) and then picks a random number and selects the item by index.</p>

	<p>I started by improving the collection method to use an ArrayList and reduced the memory usage to about 1/5th of the original method, and then rewrote the whole script to use reservoir sampling instead. <a href="http://gregable.com/2007/10/reservoir-sampling.html">Greg wrote a good explanation of reservoir sampling</a> but basically it&#8217;s an algorithm for choosing random items from a collection of unknown size without having to traverse the collection twice (to count it the first time and then to select) &#8212; in my case, the need to count the collection was requiring me to store the pipeline input in an array, so the improvement was more about memory use than about needing to enumerate the list twice. However, there <strong>is</strong> a downside: it&#8217;s slower.  <span id="more-483"></span></p>

	<h3>Reservoir Sampling</h3>

	<p>Reservoir sampling means that instead of generating a single random number and indexing into the collection, you sample as you iterate. You start by filling your &#8220;reservoir&#8221; with the first items in the pipeline, and then for each item remaining in the pipeline, you generate a random number with a decreasing probability of choosing the item. You&#8217;re trading memory usage for speed. Basically, if you want $count items, then the script stores $count elements (the eventual result) at all times. It continues processing elements until it reaches the end of the input. </p>

	<ul>
		<li>For each input element $n (where n is the count of the inputs so far) there is a $count/$n chance that it becomes part of the result.</li>
		<li>For each previously selected element, there is a $count/($n-1) chance of it remaining selected (ie: not being replaced).</li>
	</ul>
	<ul>
		<li>For the ones selected, there&#8217;s a ($count/$n * 1/$count = 1/$n) chance of it being replaced, so a ($n-1)/$n chance of it remaining &#8230; thus, it&#8217;s cumulative probability of being among the selected elements after the nth input is processed is $count/($n-1) * ($n-1)/$n = $count/$n, as it should be.</li>
	</ul>

	<p>The bottom line is that using this method is a <span class="caps">LOT</span> slower than random indexing. However, it uses a <span class="caps">LOT</span> less memory when iterating large numbers of items that are output by cmdlets as long as they weren&#8217;t already collected into an array. As a simple example, Get-ChildItem outputs each item as it traverses the provider, rather than creating an in-memory collection. If nothing in the pipeline &#8220;collects&#8221; all of the output, then it never needs to use memory except for the text display of them &#8230; or if it&#8217;s piped through a reservoir sampling algorithm, it only uses the amount of memory required to store as many items as the size of our sample.</p>

	<h3>Performance comparison</h3>

	<p>Using as an example the output of Get-ChildItem -Recurse on my profile directory, where there are about 98 thousand items total in the subdirectories, I ran each test in it&#8217;s own clean console so that I could see the results of the memory usage cleanly.  So that you could tell the difference, I changed the letters in the prompt for each test (I also use a counter in my prompt, and I change the character from > to : to make my life easier when pasting into <span class="caps">HTML</span>).  </p>

	<p>In case you&#8217;re not familiar with the commands used here, Measure-Command is a cmdlet that times how long a script takes to run, and if you wrap it in a string, it outputs a simple elapsed time statement, instead of more verbose output.  There are other ways of accomplishing the same thing, but this is the least distracting way. Also, -f is the format operator, so putting <code>&#34;{0}&#34; -f 42</code> is the same as @[string]::format(&#8221;{0}&#8221;,42) but in less<br />
code.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>A1<span style="color: #333;">&#93;</span></span>: <span style="color: #666666; font-style: italic;"># Collect the items first, and pass them as a parameter to Select-Random</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>A2<span style="color: #333;">&#93;</span></span>: <span style="color: #009900;">&quot;$(measure-command {Select-Random -count 5 -input (ls -recurse)})&quot;</span><br />
00:00:<span style="color: #cc66cc;">32.8902287</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>A3<span style="color: #333;">&#93;</span></span>: <span style="color: #009900;">&quot;{0:#,#}&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033;">ps</span> <span style="color: #003366; font-weight: bold;">powershell</span><span style="color: #66cc66;">*</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">WorkingSet</span><span style="color: #66cc66;">/</span>1KB<span style="color: #333;">&#41;</span><br />
<span style="color: #cc66cc;">189</span>,<span style="color: #cc66cc;">624</span><br />
<br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>B1<span style="color: #333;">&#93;</span></span>: <span style="color: #666666; font-style: italic;"># Pass the items via the pipeline, but use the -collect method</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>B2<span style="color: #333;">&#93;</span></span>: <span style="color: #009900;">&quot;$(measure-command {ls -recurse | Select-Random -count 5 -collect})&quot;</span><br />
00:00:<span style="color: #cc66cc;">40.6224274</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>B3<span style="color: #333;">&#93;</span></span>: <span style="color: #009900;">&quot;{0:#,#}&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033;">ps</span> <span style="color: #003366; font-weight: bold;">powershell</span><span style="color: #66cc66;">*</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">WorkingSet</span><span style="color: #66cc66;">/</span>1KB<span style="color: #333;">&#41;</span><br />
<span style="color: #cc66cc;">73</span>,<span style="color: #cc66cc;">688</span> <br />
<br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>C1<span style="color: #333;">&#93;</span></span>: <span style="color: #666666; font-style: italic;"># Pass the items on the pipeline and use reservoir sampling</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>C2<span style="color: #333;">&#93;</span></span>: <span style="color: #009900;">&quot;$(measure-command {ls -recurse | Select-Random -count 5})&quot;</span><br />
00:00:<span style="color: #cc66cc;">52.7225395</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>C3<span style="color: #333;">&#93;</span></span>: <span style="color: #009900;">&quot;{0:#,#}&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033;">ps</span> <span style="color: #003366; font-weight: bold;">powershell</span><span style="color: #66cc66;">*</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">WorkingSet</span><span style="color: #66cc66;">/</span>1KB<span style="color: #333;">&#41;</span><br />
<span style="color: #cc66cc;">43</span>,<span style="color: #cc66cc;">188</span></div>

	<p>Hopefully I didn&#8217;t make that example too complicated, and you can see not only that collecting the output into an array is faster, but that is uses a boatload more memory.  You can also see that my -collect method (which now uses an ArrayList (see the code below)) uses a lot less memory than collecting the items in an array the way the first example does.  I&#8217;m not 100% sure why this is, but my theory is that doing (ls -recurse) is in effect the same as using the += operator on an array &#8212; it makes a copy. To be more precise, it makes about 98 thousand copies.</p>

	<p>You&#8217;re not getting quite the whole picture in that example, but to fill it in a little: the memory use starts at around 39 MB &#8230; <span class="caps">JUST</span> doing the Get-ChildItem to count the files increases the working set memory usage of the PowerShell process by about 3.5 MB. Using the reservoir method to select 5 of them increased the memory usage of the PowerShell process by about 5 MB of memory (only 2 MB more than simple counting).  Using the pre-collection method requires around 151 MB. That&#8217;s a really heavy price to pay in memory, at over 1.5 KB per item in the collection.  And even the -collect method uses almost 10x as much memory.</p>

	<h3>Some perspective</h3>

	<p>To make sure you have a chance to get the same perspective I have on this &#8230; let me show you the output another way. The key thing to see in these last two examples is that the memory cost is the same for both &#8212; the same 151MB of the earlier collection example. The difference in time is large and undeniable. If you compare this with example A, you can see that basically, listing the files takes 32 seconds, and picking 25 of them takes about a tenth of a second, while iterating them (which requires generating about 98 thousand random numbers) takes about 20 seconds.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>D1<span style="color: #333;">&#93;</span></span>: <span style="color: #660033; font-weight: bold;">$files</span> <span style="color: #66cc66;">=</span> <span style="color: #660033;">ls</span> <span style="color: #000066;">-recurse</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>D2<span style="color: #333;">&#93;</span></span>: <span style="color: #009900;">&quot;$(measure-command {Select-Random -Count 25 -input $files})&quot;</span><br />
00:00:<span style="color: #cc66cc;">00.1337390</span><br />
<br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>E1<span style="color: #333;">&#93;</span></span>: <span style="color: #660033; font-weight: bold;">$files</span> <span style="color: #66cc66;">=</span> <span style="color: #660033;">ls</span> <span style="color: #000066;">-recurse</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>E2<span style="color: #333;">&#93;</span></span>: <span style="color: #009900;">&quot;$(measure-command {$files | Select-Random -Count 25})&quot;</span><br />
VERBOSE: Selected <span style="color: #cc66cc;">2</span> of <span style="color: #cc66cc;">97849</span> elements<br />
00:00:<span style="color: #cc66cc;">19.9349478</span></div>

	<h3>Conclusion</h3>

	<p>So, the point is: there are times when you need to sacrifice speed in order to keep your memory usage low &#8230; But when you&#8217;re writing a general-purpose script or a cmdlet for the PowerShell pipeline, you need to at least understand the choice you&#8217;re making so you can make it deliberately. Sometimes you&#8217;ll choose to write it fast and memory-expensive.  Sometimes you&#8217;ll choose to write it slower and memory-lean.  And sometimes, like I did here, you might choose to do both  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> .</p>

	<h5>The script &#8230; </h5>

	<p>It&#8217;s also <a href="http://powershellcentral.com/scripts/118">script 118</a> on the <a href="http://powershellcentral.com/scripts/">PowerShell central script repository</a>  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' />  (where it has a comment header that&#8217;s giving me problems here).</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">param</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;">$count</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$collectionMethod</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">array</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$inputObject</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$null</span><span style="color: #333;">&#41;</span> <br />
<br />
<span style="color: #666699; font-weight: bold;">BEGIN</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$args</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">'-?'</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
@<span style="color: #009900;">&quot;<br />
Usage: Select-Random [[-Count] &lt;int&gt;] [-inputObject] &lt;array&gt; (from pipeline) [-?]<br />
<br />
Parameters:<br />
&nbsp;-Count &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: The number of elements to select.<br />
&nbsp;-inputObject &nbsp; &nbsp; &nbsp;: The collection from which to select a random element.<br />
&nbsp;-collectionMethod : Collect the pipeline input instead of using reservoir<br />
&nbsp;-? &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Display this usage information and exit<br />
<br />
Examples:<br />
&nbsp;PS&gt; $arr = 1..5; Select-Random $arr<br />
&nbsp;PS&gt; 1..10 | Select-Random -Count 2<br />
<br />
&quot;</span>@<br />
<span style="color: #666699; font-weight: bold;">exit</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">else</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$rand</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> Random<br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$inputObject</span><span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Write-Output $inputObject | &amp;($MyInvocation.InvocationName) -Count $count</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">elseif</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$collectionMethod</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Verbose</span></span> <span style="color: #009900;">&quot;Collecting from the pipeline &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Collections.<span style="color: #003366;">ArrayList</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$inputObject</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> Collections.<span style="color: #003366;">ArrayList</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$seen</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$selected</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> object<span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span> <span style="color: #660033; font-weight: bold;">$count</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<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: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$collectionMethod</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$inputObject</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">out-<span style="font-style: normal;">null</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$seen</span><span style="color: #66cc66;">++</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$seen</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$count</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$selected</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$seen</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #666666; font-style: italic;">## For each input element $n there is a $count/$n chance that it becomes part of the result.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">elseif</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$rand</span>.<span style="color: #003366;">NextDouble</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-lt</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$count</span><span style="color: #66cc66;">/</span><span style="color: #660033; font-weight: bold;">$seen</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## For the ones previously selected, there's a 1/$n chance of it being replaced</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$selected</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$rand</span>.<span style="color: #003366;">Next</span><span style="color: #333;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #660033; font-weight: bold;">$count</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666699; font-weight: bold;">END</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span> <span style="color: #333;">&#40;</span><span style="color: #66cc66;">-</span><span style="color: #333399; font-weight: bold; font-style: italic;">not</span> <span style="color: #660033; font-weight: bold;">$inputObject</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#123;</span> &nbsp;<span style="color: #666666; font-style: italic;">## DO ONCE: (only on the re-invoke, not when using -inputObject)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Verbose</span></span> <span style="color: #009900;">&quot;Selected $count of $seen elements.&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$selected</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># foreach($el in $selected) { Write-Output $el }</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">else</span> <br />
&nbsp; &nbsp;<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Verbose</span></span> <span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;{0} elements, selecting {1}.&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #660033; font-weight: bold;">$inputObject</span>.<span style="color: #003366;">Count</span>, <span style="color: #660033; font-weight: bold;">$Count</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #666699; font-weight: bold;">in</span> 1..<span style="color: #660033; font-weight: bold;">$Count</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$inputObject</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$rand</span>.<span style="color: #003366;">Next</span><span style="color: #333;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #660033; font-weight: bold;">$inputObject</span>.<span style="color: #003366;">Count</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> &nbsp; <br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/reservoir-sampling-and-performance-considerations/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

